Skip to content

Commit

Permalink
Replace linter & fix 'psalm' errors
Browse files Browse the repository at this point in the history
  • Loading branch information
S1SYPHOS committed Oct 5, 2022
1 parent 281272a commit f6b0b51
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 89 deletions.
18 changes: 0 additions & 18 deletions .php-cs-fixer.php

This file was deleted.

8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"keywords": ["captcha", "anti-spam", "spam-protection", "spam", "obfuscation", "security", "anti-bot", "no-bot", "bot-protection"],
"homepage": "https://codeberg.org/S1SYPHOS/php-simple-captcha",
"scripts": {
"cs-dry": "php-cs-fixer fix --dry-run --diff",
"cs-fix": "php-cs-fixer fix",
"lint": "pint --verbose",
"test": "phpunit"
},
"authors": [
Expand Down Expand Up @@ -39,9 +38,10 @@
"thiagoalessio/tesseract_ocr": "Provides better results when building against OCR. Requires 'tesseract-ocr' to be installed."
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.4",
"laravel/pint": "^1.1",
"phpunit/phpunit": "^8.5|^9.0",
"mikey179/vfsstream": "^1.6"
"mikey179/vfsstream": "^1.6",
"vimeo/psalm": "^4.26"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion demo/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
F::remove($file);
}

Builder::create()->build()->save('tmp/out.jpg');;
Builder::create()->build()->save('tmp/out.jpg');
;
6 changes: 2 additions & 4 deletions demo/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@

if (isset($_SESSION['phrase']) && $captcha->compare($_SESSION['phrase'], $_POST['phrase'])) {
echo 'Captcha is valid!';
}

else {
} else {
echo 'Captcha is invalid!';
}

# Delete phrase stored in session
unset($_SESSION['phrase']);
}
?>
?>
</body>
</html>
6 changes: 2 additions & 4 deletions demo/ocr.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
for ($i = 0; $i < $total; $i++) {
echo sprintf('Captcha %s/%s ..', $i + 1, $total);

$captcha = new Builder;
$captcha = new Builder();
$captcha->distortion = false;
$captcha->build();

Expand All @@ -34,9 +34,7 @@
$captcha->save("tmp/passed-$passed.jpg");

echo ' passed!<br>';
}

else {
} else {
echo ' failed!<br>';
}

Expand Down
39 changes: 25 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php">
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Captcha Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"

bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
stderr="true"
>
<coverage ignoreDeprecatedCodeUnits="true">
<include>
<directory>./src</directory>
</include>
<exclude>
<directory>./vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<php>
<ini name="memory_limit" value="2048M" />
</php>
</phpunit>
7 changes: 7 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"preset": "psr12",
"rules": {},
"exclude": [
"src/Helpers"
]
}
16 changes: 16 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
errorLevel="7"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="src/Helpers" />
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
69 changes: 34 additions & 35 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use SimpleCaptcha\Helpers\Str;
use SimpleCaptcha\Helpers\Mime;

use \thiagoalessio\TesseractOCR\TesseractOCR;

use GdImage;
use resource;
use Exception;
Expand Down Expand Up @@ -299,25 +301,25 @@ private function drawLine(?int $color = null): void
*/
private function applyNoise(): void
{
for ($i = 0; $i < Str::length($this->phrase) * $this->noiseFactor; $i++) {
for ($i = 0; $i < Str::length($this->phrase) * $this->noiseFactor; $i++) {
# Determine random letter ..
$character = static::randomCharacter();
$character = static::randomCharacter();
$font = $this->randomFont();

# .. of random size & color, ..
$fontSize = mt_rand(5, 10);
$fontSize = mt_rand(5, 10);
$textColor = imagecolorallocate($this->image, mt_rand(0, 128), mt_rand(0, 128), mt_rand(0, 128));

# .. random position ..
$x = mt_rand(0, $this->width);
$y = mt_rand(0, $this->height);
$y = mt_rand(0, $this->height);

# .. random angle ..
$angle = mt_rand(-45, 45);
$angle = mt_rand(-45, 45);

# .. and apply it
imagettftext($this->image, $fontSize, $angle, $x, $y, $textColor, $font, $character);
}
imagettftext($this->image, $fontSize, $angle, $x, $y, $textColor, $font, $character);
}
}


Expand Down Expand Up @@ -545,7 +547,6 @@ private function distort(): void
$Vn2 = $Vn + 4 * sin($Vn / 30);
$nX = $X + ($Vx * $Vn2 / $Vn);
$nY = $Y + ($Vy * $Vn2 / $Vn);

} else {
$nX = $X;
$nY = $Y;
Expand All @@ -562,7 +563,6 @@ private function distort(): void
$this->pixel2int(floor($nX), ceil($nY)),
$this->pixel2int(ceil($nX), ceil($nY))
);

} else {
$p = $this->pixel2int($this->round($nX), $this->round($nY));
}
Expand Down Expand Up @@ -695,12 +695,12 @@ public function build(int $width = 150, int $height = 40): self
* See https://priteshgupta.com/2011/09/advanced-image-functions-using-php
* See https://github.com/raoulduke/phpocrad
*
* @param string $file Output file
* @param string $output Output file
* @param int $amount
* @param int $threshold
* @return void
*/
private function img2ocr(?string $output = null, int $amount = 80, int $threshold = 3): void
private function img2ocr(string $output, int $amount = 80, int $threshold = 3): void
{
$image = $this->image;

Expand Down Expand Up @@ -752,9 +752,7 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol
}
}
}
}

else {
} else {
for ($x = 0; $x < $this->width; $x++) { # each row
for ($y = 0; $y < $this->height; $y++) { # each pixel
$rgbOrig = imagecolorat($image, $x, $y);
Expand All @@ -770,14 +768,23 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol
$bBlur = ($rgbBlur & 0xFF);

$rNew = ($amount * ($rOrig - $rBlur)) + $rOrig;
if ($rNew > 255) { $rNew = 255; }
elseif ($rNew < 0) { $rNew = 0; }
if ($rNew > 255) {
$rNew = 255;
} elseif ($rNew < 0) {
$rNew = 0;
}
$gNew = ($amount * ($gOrig - $gBlur)) + $gOrig;
if ($gNew > 255) { $gNew = 255; }
elseif ($gNew < 0) { $gNew = 0; }
if ($gNew > 255) {
$gNew = 255;
} elseif ($gNew < 0) {
$gNew = 0;
}
$bNew = ($amount * ($bOrig - $bBlur)) + $bOrig;
if ($bNew > 255) { $bNew = 255; }
elseif ($bNew < 0) { $bNew = 0; }
if ($bNew > 255) {
$bNew = 255;
} elseif ($bNew < 0) {
$bNew = 0;
}
$rgbNew = ($rNew << 16) + ($gNew << 8) + $bNew;

imagesetpixel($image, $x, $y, $rgbNew);
Expand All @@ -802,15 +809,11 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol
$green = $this->round(0.59 * $colors['green']);
$blue = $this->round(0.11 * $colors['blue']);

# Create single-byte string from them
# Create single-byte string from them
$pgm .= chr($red + $green + $blue);
}
}

if (empty($output)) {
$output = sprintf('%s/%s.pgm', F::dirname($file), F::name($file));
}

F::write($output, $pgm);
}

Expand Down Expand Up @@ -858,9 +861,9 @@ public function isOCRReadable(string $tmpDir = '.tmp'): bool
# Iterate over available modes ..
foreach ($modes as $mode) {
# .. using (suggested) external library (if available), otherwise ..
if ($mode == 'tesseract' && class_exists('\thiagoalessio\TesseractOCR\TesseractOCR')) {
if ($mode == 'tesseract' && class_exists(TesseractOCR::class)) {
# Execute `tesseract-ocr-for-php` & store its output
$tesseract = new \thiagoalessio\TesseractOCR\TesseractOCR($pgmFile);
$tesseract = new TesseractOCR($pgmFile);
$outputs[] = $tesseract->allowlist(range(0, 9), range('a', 'z'), range('A', 'Z'))->dpi(2200)->run();
}

Expand Down Expand Up @@ -956,13 +959,9 @@ protected function gd2img(int $quality = 90, ?string $filename = null, string $t

if ($type == 'gif') {
imagegif($this->image, $filename);
}

elseif ($type == 'jpg') {
} elseif ($type == 'jpg') {
imagejpeg($this->image, $filename, $quality);
}

elseif ($type == 'png') {
} elseif ($type == 'png') {
# Normalize quality
if ($quality > 9) {
$quality = -1;
Expand Down Expand Up @@ -1041,7 +1040,7 @@ public function inline(int $quality = 90, string $type = 'jpg'): string

/**
* Rounds float to integer
*
*
* @param float $number
* @return int
*/
Expand All @@ -1053,7 +1052,7 @@ private function round(float $number): int

/**
* Creates random float between two digits
*
*
* @param float|int $min
* @param float|int $max
* @return float
Expand Down
13 changes: 7 additions & 6 deletions src/BuilderAbstract.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

declare(strict_types=1);

namespace SimpleCaptcha;

use SimpleCaptcha\Helpers\Str;


/**
* Class BuilderAbstract
*
Expand Down Expand Up @@ -43,7 +44,7 @@ abstract class BuilderAbstract
* @param int $height Captcha image height
* @return self
*/
public abstract function build(int $width, int $height): self;
abstract public function build(int $width, int $height): self;


/**
Expand All @@ -53,7 +54,7 @@ public abstract function build(int $width, int $height): self;
* @param int $quality Captcha image quality
* @return void
*/
public abstract function save(string $filename, int $quality = 90): void;
abstract public function save(string $filename, int $quality = 90): void;


/**
Expand All @@ -63,7 +64,7 @@ public abstract function save(string $filename, int $quality = 90): void;
* @param string $type Captcha image output format
* @return void
*/
public abstract function output(int $quality = 90, string $type = 'jpg'): void;
abstract public function output(int $quality = 90, string $type = 'jpg'): void;


/**
Expand All @@ -73,7 +74,7 @@ public abstract function output(int $quality = 90, string $type = 'jpg'): void;
* @param string $type Captcha image output format
* @return string
*/
public abstract function fetch(int $quality = 90, string $type = 'jpg'): string;
abstract public function fetch(int $quality = 90, string $type = 'jpg'): string;


/**
Expand All @@ -83,7 +84,7 @@ public abstract function fetch(int $quality = 90, string $type = 'jpg'): string;
* @param string $type Captcha image output format
* @return string
*/
public abstract function inline(int $quality = 90, string $type = 'jpg'): string;
abstract public function inline(int $quality = 90, string $type = 'jpg'): string;


/**
Expand Down
Loading

0 comments on commit f6b0b51

Please sign in to comment.