Skip to content

Commit f6b0b51

Browse files
committed
Replace linter & fix 'psalm' errors
1 parent 281272a commit f6b0b51

File tree

13 files changed

+101
-89
lines changed

13 files changed

+101
-89
lines changed

.php-cs-fixer.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"keywords": ["captcha", "anti-spam", "spam-protection", "spam", "obfuscation", "security", "anti-bot", "no-bot", "bot-protection"],
88
"homepage": "https://codeberg.org/S1SYPHOS/php-simple-captcha",
99
"scripts": {
10-
"cs-dry": "php-cs-fixer fix --dry-run --diff",
11-
"cs-fix": "php-cs-fixer fix",
10+
"lint": "pint --verbose",
1211
"test": "phpunit"
1312
},
1413
"authors": [
@@ -39,9 +38,10 @@
3938
"thiagoalessio/tesseract_ocr": "Provides better results when building against OCR. Requires 'tesseract-ocr' to be installed."
4039
},
4140
"require-dev": {
42-
"friendsofphp/php-cs-fixer": "^3.4",
41+
"laravel/pint": "^1.1",
4342
"phpunit/phpunit": "^8.5|^9.0",
44-
"mikey179/vfsstream": "^1.6"
43+
"mikey179/vfsstream": "^1.6",
44+
"vimeo/psalm": "^4.26"
4545
},
4646
"autoload": {
4747
"psr-4": {

demo/demo.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
F::remove($file);
1515
}
1616

17-
Builder::create()->build()->save('tmp/out.jpg');;
17+
Builder::create()->build()->save('tmp/out.jpg');
18+
;

demo/form.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@
2828

2929
if (isset($_SESSION['phrase']) && $captcha->compare($_SESSION['phrase'], $_POST['phrase'])) {
3030
echo 'Captcha is valid!';
31-
}
32-
33-
else {
31+
} else {
3432
echo 'Captcha is invalid!';
3533
}
3634

3735
# Delete phrase stored in session
3836
unset($_SESSION['phrase']);
3937
}
40-
?>
38+
?>
4139
</body>
4240
</html>

demo/ocr.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
for ($i = 0; $i < $total; $i++) {
2424
echo sprintf('Captcha %s/%s ..', $i + 1, $total);
2525

26-
$captcha = new Builder;
26+
$captcha = new Builder();
2727
$captcha->distortion = false;
2828
$captcha->build();
2929

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

3636
echo ' passed!<br>';
37-
}
38-
39-
else {
37+
} else {
4038
echo ' failed!<br>';
4139
}
4240

phpunit.xml

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
3-
<coverage>
4-
<include>
5-
<directory>./</directory>
6-
</include>
7-
<exclude>
8-
<directory>./vendor</directory>
9-
</exclude>
10-
</coverage>
11-
<testsuites>
12-
<testsuite name="Captcha Test Suite">
13-
<directory>./tests/</directory>
14-
</testsuite>
15-
</testsuites>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
5+
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
verbose="true"
9+
stderr="true"
10+
>
11+
<coverage ignoreDeprecatedCodeUnits="true">
12+
<include>
13+
<directory>./src</directory>
14+
</include>
15+
<exclude>
16+
<directory>./vendor</directory>
17+
</exclude>
18+
</coverage>
19+
<testsuites>
20+
<testsuite name="Tests">
21+
<directory>./tests/</directory>
22+
</testsuite>
23+
</testsuites>
24+
<php>
25+
<ini name="memory_limit" value="2048M" />
26+
</php>
1627
</phpunit>

pint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"preset": "psr12",
3+
"rules": {},
4+
"exclude": [
5+
"src/Helpers"
6+
]
7+
}

psalm.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="7"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="src/Helpers" />
13+
<directory name="vendor" />
14+
</ignoreFiles>
15+
</projectFiles>
16+
</psalm>

src/Builder.php

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use SimpleCaptcha\Helpers\Str;
1111
use SimpleCaptcha\Helpers\Mime;
1212

13+
use \thiagoalessio\TesseractOCR\TesseractOCR;
14+
1315
use GdImage;
1416
use resource;
1517
use Exception;
@@ -299,25 +301,25 @@ private function drawLine(?int $color = null): void
299301
*/
300302
private function applyNoise(): void
301303
{
302-
for ($i = 0; $i < Str::length($this->phrase) * $this->noiseFactor; $i++) {
304+
for ($i = 0; $i < Str::length($this->phrase) * $this->noiseFactor; $i++) {
303305
# Determine random letter ..
304-
$character = static::randomCharacter();
306+
$character = static::randomCharacter();
305307
$font = $this->randomFont();
306308

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

311313
# .. random position ..
312314
$x = mt_rand(0, $this->width);
313-
$y = mt_rand(0, $this->height);
315+
$y = mt_rand(0, $this->height);
314316

315317
# .. random angle ..
316-
$angle = mt_rand(-45, 45);
318+
$angle = mt_rand(-45, 45);
317319

318320
# .. and apply it
319-
imagettftext($this->image, $fontSize, $angle, $x, $y, $textColor, $font, $character);
320-
}
321+
imagettftext($this->image, $fontSize, $angle, $x, $y, $textColor, $font, $character);
322+
}
321323
}
322324

323325

@@ -545,7 +547,6 @@ private function distort(): void
545547
$Vn2 = $Vn + 4 * sin($Vn / 30);
546548
$nX = $X + ($Vx * $Vn2 / $Vn);
547549
$nY = $Y + ($Vy * $Vn2 / $Vn);
548-
549550
} else {
550551
$nX = $X;
551552
$nY = $Y;
@@ -562,7 +563,6 @@ private function distort(): void
562563
$this->pixel2int(floor($nX), ceil($nY)),
563564
$this->pixel2int(ceil($nX), ceil($nY))
564565
);
565-
566566
} else {
567567
$p = $this->pixel2int($this->round($nX), $this->round($nY));
568568
}
@@ -695,12 +695,12 @@ public function build(int $width = 150, int $height = 40): self
695695
* See https://priteshgupta.com/2011/09/advanced-image-functions-using-php
696696
* See https://github.com/raoulduke/phpocrad
697697
*
698-
* @param string $file Output file
698+
* @param string $output Output file
699699
* @param int $amount
700700
* @param int $threshold
701701
* @return void
702702
*/
703-
private function img2ocr(?string $output = null, int $amount = 80, int $threshold = 3): void
703+
private function img2ocr(string $output, int $amount = 80, int $threshold = 3): void
704704
{
705705
$image = $this->image;
706706

@@ -752,9 +752,7 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol
752752
}
753753
}
754754
}
755-
}
756-
757-
else {
755+
} else {
758756
for ($x = 0; $x < $this->width; $x++) { # each row
759757
for ($y = 0; $y < $this->height; $y++) { # each pixel
760758
$rgbOrig = imagecolorat($image, $x, $y);
@@ -770,14 +768,23 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol
770768
$bBlur = ($rgbBlur & 0xFF);
771769

772770
$rNew = ($amount * ($rOrig - $rBlur)) + $rOrig;
773-
if ($rNew > 255) { $rNew = 255; }
774-
elseif ($rNew < 0) { $rNew = 0; }
771+
if ($rNew > 255) {
772+
$rNew = 255;
773+
} elseif ($rNew < 0) {
774+
$rNew = 0;
775+
}
775776
$gNew = ($amount * ($gOrig - $gBlur)) + $gOrig;
776-
if ($gNew > 255) { $gNew = 255; }
777-
elseif ($gNew < 0) { $gNew = 0; }
777+
if ($gNew > 255) {
778+
$gNew = 255;
779+
} elseif ($gNew < 0) {
780+
$gNew = 0;
781+
}
778782
$bNew = ($amount * ($bOrig - $bBlur)) + $bOrig;
779-
if ($bNew > 255) { $bNew = 255; }
780-
elseif ($bNew < 0) { $bNew = 0; }
783+
if ($bNew > 255) {
784+
$bNew = 255;
785+
} elseif ($bNew < 0) {
786+
$bNew = 0;
787+
}
781788
$rgbNew = ($rNew << 16) + ($gNew << 8) + $bNew;
782789

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

805-
# Create single-byte string from them
812+
# Create single-byte string from them
806813
$pgm .= chr($red + $green + $blue);
807814
}
808815
}
809816

810-
if (empty($output)) {
811-
$output = sprintf('%s/%s.pgm', F::dirname($file), F::name($file));
812-
}
813-
814817
F::write($output, $pgm);
815818
}
816819

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

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

957960
if ($type == 'gif') {
958961
imagegif($this->image, $filename);
959-
}
960-
961-
elseif ($type == 'jpg') {
962+
} elseif ($type == 'jpg') {
962963
imagejpeg($this->image, $filename, $quality);
963-
}
964-
965-
elseif ($type == 'png') {
964+
} elseif ($type == 'png') {
966965
# Normalize quality
967966
if ($quality > 9) {
968967
$quality = -1;
@@ -1041,7 +1040,7 @@ public function inline(int $quality = 90, string $type = 'jpg'): string
10411040

10421041
/**
10431042
* Rounds float to integer
1044-
*
1043+
*
10451044
* @param float $number
10461045
* @return int
10471046
*/
@@ -1053,7 +1052,7 @@ private function round(float $number): int
10531052

10541053
/**
10551054
* Creates random float between two digits
1056-
*
1055+
*
10571056
* @param float|int $min
10581057
* @param float|int $max
10591058
* @return float

src/BuilderAbstract.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace SimpleCaptcha;
46

57
use SimpleCaptcha\Helpers\Str;
68

7-
89
/**
910
* Class BuilderAbstract
1011
*
@@ -43,7 +44,7 @@ abstract class BuilderAbstract
4344
* @param int $height Captcha image height
4445
* @return self
4546
*/
46-
public abstract function build(int $width, int $height): self;
47+
abstract public function build(int $width, int $height): self;
4748

4849

4950
/**
@@ -53,7 +54,7 @@ public abstract function build(int $width, int $height): self;
5354
* @param int $quality Captcha image quality
5455
* @return void
5556
*/
56-
public abstract function save(string $filename, int $quality = 90): void;
57+
abstract public function save(string $filename, int $quality = 90): void;
5758

5859

5960
/**
@@ -63,7 +64,7 @@ public abstract function save(string $filename, int $quality = 90): void;
6364
* @param string $type Captcha image output format
6465
* @return void
6566
*/
66-
public abstract function output(int $quality = 90, string $type = 'jpg'): void;
67+
abstract public function output(int $quality = 90, string $type = 'jpg'): void;
6768

6869

6970
/**
@@ -73,7 +74,7 @@ public abstract function output(int $quality = 90, string $type = 'jpg'): void;
7374
* @param string $type Captcha image output format
7475
* @return string
7576
*/
76-
public abstract function fetch(int $quality = 90, string $type = 'jpg'): string;
77+
abstract public function fetch(int $quality = 90, string $type = 'jpg'): string;
7778

7879

7980
/**
@@ -83,7 +84,7 @@ public abstract function fetch(int $quality = 90, string $type = 'jpg'): string;
8384
* @param string $type Captcha image output format
8485
* @return string
8586
*/
86-
public abstract function inline(int $quality = 90, string $type = 'jpg'): string;
87+
abstract public function inline(int $quality = 90, string $type = 'jpg'): string;
8788

8889

8990
/**

0 commit comments

Comments
 (0)