From bd98375dc9043fe20685760ad057e3509d0519c4 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Sat, 6 Jan 2024 14:27:24 +0100 Subject: [PATCH] added and fixed typehints, removed deprecations --- CHANGELOG.md | 1 + phpcs.xml.dist | 2 ++ src/Entity.php | 5 ++--- src/Event/LinkScannerCommandEventListener.php | 8 ++++---- src/ResultScan.php | 2 +- src/ScanEntity.php | 2 +- src/TestSuite/IntegrationTestTrait.php | 2 +- src/Utility/BodyParser.php | 3 ++- src/Utility/LinkScanner.php | 8 ++++---- tests/TestCase/Command/LinkScannerCommandTest.php | 4 +--- tests/TestCase/Utility/LinkScannerTest.php | 2 +- 11 files changed, 20 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 550656e..4504d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 1.2.0 * requires at least PHP 8.1, CakePHP 5.0 and PHPUnit 10; * provides its own `phpUri` library; +* added and fixed typehints, removed deprecations; * added tests for PHP 8.3. ## 1.1 branch diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 2bb84c4..3845390 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -11,4 +11,6 @@ 0 + + libs/phpuri\.php diff --git a/src/Entity.php b/src/Entity.php index c536410..9f3bf33 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -90,7 +90,7 @@ public function hasValue(string $property): bool * @param mixed $default Default value if the property does not exist * @return mixed Property value */ - public function get(string $property, $default = null) + public function get(string $property, mixed $default = null): mixed { return $this->has($property) ? $this->properties[$property] : $default; } @@ -121,8 +121,7 @@ public function offsetExists($offset): bool * @param EntityPropertyName $offset The offset to get * @return mixed */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet($offset): mixed { return $this->properties[$offset]; } diff --git a/src/Event/LinkScannerCommandEventListener.php b/src/Event/LinkScannerCommandEventListener.php index 246a990..a59847d 100644 --- a/src/Event/LinkScannerCommandEventListener.php +++ b/src/Event/LinkScannerCommandEventListener.php @@ -19,7 +19,7 @@ use Cake\Console\ConsoleIo; use Cake\Event\Event; use Cake\Http\Client\Response; -use Cake\I18n\FrozenTime; +use Cake\I18n\DateTime; use LinkScanner\ResultScan; use function Cake\I18n\__d; @@ -167,8 +167,8 @@ public function scanCompleted(Event $event, int $startTime, int $endTime, Result $this->io->hr(); } - $endTime = new FrozenTime($endTime); - $elapsedTime = $endTime->diffForHumans(new FrozenTime($startTime), true); + $endTime = new DateTime($endTime); + $elapsedTime = $endTime->diffForHumans(new DateTime($startTime), true); $this->io->out(__d('link-scanner', 'Scan completed at {0}', $endTime->i18nFormat('yyyy-MM-dd HH:mm:ss'))); $this->io->out(__d('link-scanner', 'Elapsed time: {0}', $elapsedTime)); @@ -197,7 +197,7 @@ public function scanStarted(Event $event, int $startTime, string $fullBaseUrl): $this->io->hr(); } - $startTime = (new FrozenTime($startTime))->i18nFormat('yyyy-MM-dd HH:mm:ss'); + $startTime = (new DateTime($startTime))->i18nFormat('yyyy-MM-dd HH:mm:ss'); $this->io->info(__d('link-scanner', 'Scan started for {0} at {1}', $fullBaseUrl, $startTime)); if (!$this->args->getOption('verbose')) { diff --git a/src/ResultScan.php b/src/ResultScan.php index 1251466..5d4992c 100644 --- a/src/ResultScan.php +++ b/src/ResultScan.php @@ -57,7 +57,7 @@ public function __construct(CollectionInterface|iterable $items = []) * @param iterable $items Items * @return \Cake\Collection\CollectionInterface */ - public function append($items): CollectionInterface + public function append(iterable $items): CollectionInterface { return new ResultScan(array_merge($this->buffered()->toArray(), $this->parseItems($items))); } diff --git a/src/ScanEntity.php b/src/ScanEntity.php index 1d27631..87270ea 100644 --- a/src/ScanEntity.php +++ b/src/ScanEntity.php @@ -57,7 +57,7 @@ public function __construct(array $properties = []) * @see \Cake\Http\Client\Response * @thrown \BadMethodCallException */ - public function __call(string $name, $arguments) + public function __call(string $name, mixed $arguments): mixed { if (method_exists(Response::class, $name)) { $Response = (new Response()) diff --git a/src/TestSuite/IntegrationTestTrait.php b/src/TestSuite/IntegrationTestTrait.php index 8a32246..d35acb3 100644 --- a/src/TestSuite/IntegrationTestTrait.php +++ b/src/TestSuite/IntegrationTestTrait.php @@ -68,7 +68,7 @@ protected function getClientReturnsFromTests(): Client * @param string|array|null $fullBaseUrl Full base url * @return \LinkScanner\Utility\LinkScanner&\PHPUnit\Framework\MockObject\MockObject */ - protected function getLinkScannerClientReturnsFromTests($fullBaseUrl = null): LinkScanner + protected function getLinkScannerClientReturnsFromTests(string|array|null $fullBaseUrl = null): LinkScanner { $fullBaseUrl = $fullBaseUrl ?: Configure::read('App.fullBaseUrl', 'http://localhost'); $fullBaseUrl = is_string($fullBaseUrl) ? $fullBaseUrl : Router::url($fullBaseUrl, true); diff --git a/src/Utility/BodyParser.php b/src/Utility/BodyParser.php index 09d4230..80366b8 100644 --- a/src/Utility/BodyParser.php +++ b/src/Utility/BodyParser.php @@ -15,6 +15,7 @@ namespace LinkScanner\Utility; use phpUri; +use Psr\Http\Message\StreamInterface; use Symfony\Component\DomCrawler\Crawler; /** @@ -69,7 +70,7 @@ class BodyParser * @param string|\Psr\Http\Message\StreamInterface $body Body * @param string $url Reference url. Used to determine the relative links */ - public function __construct($body, string $url) + public function __construct(StreamInterface|string $body, string $url) { $this->body = (string)$body; $this->url = $url; diff --git a/src/Utility/LinkScanner.php b/src/Utility/LinkScanner.php index 3938451..5b4cb58 100644 --- a/src/Utility/LinkScanner.php +++ b/src/Utility/LinkScanner.php @@ -128,7 +128,7 @@ public function __construct(?Client $Client = null, ?ResultScan $ResultScan = nu * @param string $name Property name * @return mixed */ - public function __get(string $name) + public function __get(string $name): mixed { return $this->{$name}; } @@ -334,7 +334,7 @@ public function serialize(): string * @param string $data The string representation of the object * @return void */ - public function unserialize($data): void + public function unserialize(string $data): void { //Resets the event list and the Client instance $properties = unserialize($data); @@ -396,9 +396,9 @@ public function import(string $filename) try { if (!is_readable($filename)) { - throw new LogicException(__d('link-scanner', 'File or directory `'. $filename . '` is not readable')); + throw new LogicException(__d('link-scanner', 'File or directory `' . $filename . '` is not readable')); } - $instance = unserialize(file_get_contents($filename)); + $instance = unserialize(file_get_contents($filename) ?: ''); } catch (Throwable $e) { $message = preg_replace('/file_get_contents\([^)]+\):\s+/', '', $e->getMessage()) ?: ''; throw new LogicException( diff --git a/tests/TestCase/Command/LinkScannerCommandTest.php b/tests/TestCase/Command/LinkScannerCommandTest.php index 92b3a9f..4cb2e83 100644 --- a/tests/TestCase/Command/LinkScannerCommandTest.php +++ b/tests/TestCase/Command/LinkScannerCommandTest.php @@ -1,6 +1,4 @@ fullBaseUrl))); - return substr($line, 0, strlen('Checking')) === 'Checking' && !preg_match($pattern, $line); + return str_starts_with($line, 'Checking') && !preg_match($pattern, $line); }; $this->assertEmpty(array_filter($this->_out->messages(), $differentLines)); $this->assertOutputContains('Scanning of external links is not enabled'); diff --git a/tests/TestCase/Utility/LinkScannerTest.php b/tests/TestCase/Utility/LinkScannerTest.php index 71b9c99..22f2697 100644 --- a/tests/TestCase/Utility/LinkScannerTest.php +++ b/tests/TestCase/Utility/LinkScannerTest.php @@ -221,7 +221,7 @@ public function testImport(): void //With a no existing file $filename = TMP . 'noExistingDir' . DS . 'result'; - $this->expectExceptionMessage('Failed to import results from file `' . $filename . '` with message `file or directory `'. $filename . '` is not readable`'); + $this->expectExceptionMessage('Failed to import results from file `' . $filename . '` with message `file or directory `' . $filename . '` is not readable`'); $this->LinkScanner->import($filename); }