From 8b659904ef487c846db551b6a68638b119abd8d4 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Thu, 22 Sep 2022 15:49:58 +0200 Subject: [PATCH 1/5] fixed --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 337c225..4312ae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # 1.x branch ## 1.1 branch ### 1.1.6 -* added tests for PHP 8.1. +* added tests for PHP 8.1; +* little fixes for `phpstan`, `psalm` and for the `composer.json` file. ### 1.1.5 * updated for `php-tools` 1.5.8. From df7f2c3323267d88dc89863eaaf1b7a503246292 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 14 Oct 2022 18:56:19 +0200 Subject: [PATCH 2/5] updated --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4da99c4..1cf6009 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .directory +.idea .phpunit.result.cache *_old *.old.* From 4fe25c7cb72045850c47152827bfd76c827201a8 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 14 Oct 2022 18:56:22 +0200 Subject: [PATCH 3/5] updated for `php-tools` 1.7.0 --- CHANGELOG.md | 3 +++ phpstan-baseline.neon | 5 ----- src/SpamDetector.php | 8 +++----- tests/TestCase/SpamDetectorTest.php | 29 ++++++++++++++--------------- version | 2 +- 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 337c225..51b9d53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # 1.x branch ## 1.1 branch +### 1.1.7 +* updated for `php-tools` 1.7.0. + ### 1.1.6 * added tests for PHP 8.1. diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6283052..99a50f6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5,8 +5,3 @@ parameters: count: 1 path: tests/TestCase/SpamDetectorTest.php - - - message: "#^Ternary operator condition is always true\\.$#" - count: 1 - path: tests/TestCase/SpamDetectorTest.php - diff --git a/src/SpamDetector.php b/src/SpamDetector.php index 681f9a0..c22d1ee 100644 --- a/src/SpamDetector.php +++ b/src/SpamDetector.php @@ -14,7 +14,6 @@ */ namespace StopSpam; -use BadMethodCallException; use Cake\Cache\Cache; use Cake\Core\InstanceConfigTrait; use Cake\Http\Client; @@ -73,14 +72,13 @@ public function __construct(?Client $Client = null) * @param string $name Method name * @param array $arguments Method arguments * @return $this - * @throws \BadMethodCallException - * @uses $data + * @throws \Tools\Exception\NotInArrayException|\ErrorException */ public function __call(string $name, array $arguments) { $methodName = sprintf('%s::%s', get_class($this), $name); - Exceptionist::inArray($name, ['email', 'ip', 'username'], __d('stop-spam', 'Method `{0}()` does not exist', $methodName), BadMethodCallException::class); - Exceptionist::isTrue($arguments, __d('stop-spam', 'At least 1 argument required for `{0}()` method', $methodName), BadMethodCallException::class); + Exceptionist::inArray($name, ['email', 'ip', 'username'], __d('stop-spam', 'Method `{0}()` does not exist', $methodName)); + Exceptionist::isTrue($arguments, __d('stop-spam', 'At least 1 argument required for `{0}()` method', $methodName)); $this->data[$name] = array_merge($this->data[$name] ?? [], $arguments); diff --git a/tests/TestCase/SpamDetectorTest.php b/tests/TestCase/SpamDetectorTest.php index 6bc9442..450568d 100644 --- a/tests/TestCase/SpamDetectorTest.php +++ b/tests/TestCase/SpamDetectorTest.php @@ -14,7 +14,6 @@ */ namespace StopSpam\Test\TestCase; -use BadMethodCallException; use Cake\Cache\Cache; use Cake\Http\Client; use Cake\Http\Client\Response; @@ -30,7 +29,7 @@ class SpamDetectorTest extends TestCase /** * @var \StopSpam\SpamDetector */ - protected $SpamDetector; + protected SpamDetector $SpamDetector; /** * Called before every test method @@ -41,26 +40,28 @@ public function setUp(): void parent::setUp(); $Client = $this->getMockBuilder(Client::class) - ->setMethods(['get']) + ->onlyMethods(['get']) ->getMock(); $Client->expects($this->any()) ->method('get') - ->will($this->returnCallback(function (string $url, $data = []): Response { + ->willReturnCallback(function (string $url, $data = []): Response { //Gets the `Response` instance already saved in the test files $file = TESTS . DS . 'responses' . DS . md5(serialize($data)); - if (!file_exists($file)) { - echo PHP_EOL . 'Creating file `' . $file . '`...' . PHP_EOL; - $response = (new Client())->get($url, $data); - file_put_contents($file, $response->getStringBody()); - - return $response; + if (file_exists($file)) { + return new Response([], file_get_contents($file) ?: ''); } - return new Response([], file_get_contents($file) ?: ''); - })); + echo PHP_EOL . 'Creating file `' . $file . '`...' . PHP_EOL; + $response = (new Client())->get($url, $data); + file_put_contents($file, $response->getStringBody()); + + return $response; + }); - $this->SpamDetector = $this->SpamDetector ?: new SpamDetector($Client); + if (empty($this->SpamDetector)) { + $this->SpamDetector = new SpamDetector($Client); + } } /** @@ -80,7 +81,6 @@ public function tearDown(): void */ public function testCallMagicMethodNoExistingMethod(): void { - $this->expectException(BadMethodCallException::class); $this->expectExceptionMessage('Method `StopSpam\SpamDetector::noExisting()` does not exist'); (new SpamDetector())->noExisting(); } @@ -91,7 +91,6 @@ public function testCallMagicMethodNoExistingMethod(): void */ public function testCallMagicMethodMissingArguments(): void { - $this->expectException(BadMethodCallException::class); $this->expectExceptionMessage('At least 1 argument required for `StopSpam\SpamDetector::username()` method'); (new SpamDetector())->username(); } diff --git a/version b/version index 0664a8f..2bf1ca5 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.1.6 +1.1.7 From be01e05092b9a936d7accab25cc0f90b4821e98a Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 14 Oct 2022 19:03:52 +0200 Subject: [PATCH 4/5] little code fixes --- CHANGELOG.md | 3 ++- phpstan-baseline.neon | 5 +++++ src/SpamDetector.php | 2 +- tests/TestCase/SpamDetectorTest.php | 10 ++++++---- tests/test_app/TestApp/Application.php | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee16ae6..507f738 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # 1.x branch ## 1.1 branch ### 1.1.7 -* updated for `php-tools` 1.7.0. +* updated for `php-tools` 1.7.0; +* little code fixes. ### 1.1.6 * added tests for PHP 8.1; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 99a50f6..b6014f3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5,3 +5,8 @@ parameters: count: 1 path: tests/TestCase/SpamDetectorTest.php + - + message: "#^Negated boolean expression is always false\\.$#" + count: 1 + path: tests/TestCase/SpamDetectorTest.php + diff --git a/src/SpamDetector.php b/src/SpamDetector.php index c22d1ee..3868e19 100644 --- a/src/SpamDetector.php +++ b/src/SpamDetector.php @@ -25,7 +25,7 @@ * A spam detector * @method \StopSpam\SpamDetector email(string ...$email) Sets an email address to verify * @method \StopSpam\SpamDetector ip(string ...$ip) Sets an IP address to verify - * @method \StopSpam\SpamDetector username(string ...$username) Sets an username to verify + * @method \StopSpam\SpamDetector username(string ...$username) Sets a username to verify */ class SpamDetector { diff --git a/tests/TestCase/SpamDetectorTest.php b/tests/TestCase/SpamDetectorTest.php index 450568d..7d0cd2e 100644 --- a/tests/TestCase/SpamDetectorTest.php +++ b/tests/TestCase/SpamDetectorTest.php @@ -1,4 +1,5 @@ expects($this->any()) ->method('get') - ->willReturnCallback(function (string $url, $data = []): Response { + ->willReturnCallback(function (string $url, array $data = []): Response { //Gets the `Response` instance already saved in the test files $file = TESTS . DS . 'responses' . DS . md5(serialize($data)); if (file_exists($file)) { @@ -59,7 +60,7 @@ public function setUp(): void return $response; }); - if (empty($this->SpamDetector)) { + if (!$this->SpamDetector) { $this->SpamDetector = new SpamDetector($Client); } } @@ -82,6 +83,7 @@ public function tearDown(): void public function testCallMagicMethodNoExistingMethod(): void { $this->expectExceptionMessage('Method `StopSpam\SpamDetector::noExisting()` does not exist'); + /** @noinspection PhpUndefinedMethodInspection */ (new SpamDetector())->noExisting(); } @@ -185,7 +187,7 @@ public function testVerify(): void public function testVerifyWithErrorFromServer(): void { $SpamDetector = @$this->getMockBuilder(SpamDetector::class) - ->setMethods(['_getResponse']) + ->onlyMethods(['_getResponse']) ->getMock(); $SpamDetector->method('_getResponse')->willReturn([ 'success' => 0, diff --git a/tests/test_app/TestApp/Application.php b/tests/test_app/TestApp/Application.php index f098d8b..84f9ffe 100644 --- a/tests/test_app/TestApp/Application.php +++ b/tests/test_app/TestApp/Application.php @@ -15,7 +15,7 @@ public function bootstrap(): void $this->addPlugin(StopSpam::class); } - public function middleware(MiddlewareQueue $middleware) + public function middleware(MiddlewareQueue $middleware): MiddlewareQueue { return $middleware->add(new RoutingMiddleware($this)); } From 327cca1a46da68a9914c8b109f01f0ea5d04dcfb Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 14 Oct 2022 19:06:53 +0200 Subject: [PATCH 5/5] fixed --- src/SpamDetector.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/SpamDetector.php b/src/SpamDetector.php index 3868e19..1a6948f 100644 --- a/src/SpamDetector.php +++ b/src/SpamDetector.php @@ -18,7 +18,7 @@ use Cake\Core\InstanceConfigTrait; use Cake\Http\Client; use Cake\Utility\Hash; -use Exception; +use ErrorException; use Tools\Exceptionist; /** @@ -60,7 +60,6 @@ class SpamDetector /** * Construct * @param \Cake\Http\Client|null $Client A Client instance - * @uses $Client */ public function __construct(?Client $Client = null) { @@ -89,7 +88,6 @@ public function __call(string $name, array $arguments) * Performs a single GET request and returns result * @param array $data The query data you want to send * @return array Result - * @uses $Client */ protected function _getResponse(array $data): array { @@ -103,7 +101,6 @@ protected function _getResponse(array $data): array /** * Returns results of the last verification * @return array - * @uses $result */ public function getResult(): array { @@ -112,12 +109,8 @@ public function getResult(): array /** * Verifies, based on the set data, if it's a spammer - * @return bool Returns `false` if certainly at least one of the parameters - * has been reported as a spammer, otherwise returns `true` - * @throws \Exception - * @uses _getResponse() - * @uses $data - * @uses $result + * @return bool Returns `false` if certainly at least one of the parameters has been reported as a spammer + * @throws \ErrorException */ public function verify(): bool { @@ -125,7 +118,7 @@ public function verify(): bool $this->result = $this->_getResponse($this->data); if (array_key_exists('error', $this->result)) { - throw new Exception(__d('stop-spam', 'Error from server: `{0}`', $this->result['error'])); + throw new ErrorException(__d('stop-spam', 'Error from server: `{0}`', $this->result['error'])); } $this->data = [];