Skip to content

Commit

Permalink
added and fixed typehints, removed deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
mirko-pagliai committed Jan 6, 2024
1 parent d734dc8 commit bd98375
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace.UseFromSameNamespace">
<severity>0</severity>
</rule>

<exclude-pattern>libs/phpuri\.php</exclude-pattern>
</ruleset>
5 changes: 2 additions & 3 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
}
Expand Down
8 changes: 4 additions & 4 deletions src/Event/LinkScannerCommandEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion src/ResultScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/ScanEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/TestSuite/IntegrationTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/Utility/BodyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace LinkScanner\Utility;

use phpUri;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\DomCrawler\Crawler;

/**
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/Utility/LinkScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions tests/TestCase/Command/LinkScannerCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php
/** @noinspection ALL */
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);

/**
Expand Down Expand Up @@ -220,7 +218,7 @@ public function testExecuteWithSomeParameters(): void
$differentLines = function (string $line): bool {
$pattern = sprintf('/^Checking https?:\/\/%s/', preg_quote(get_hostname_from_url($this->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));

Check failure on line 223 in tests/TestCase/Command/LinkScannerCommandTest.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Cannot call method messages() on Cake\Console\TestSuite\StubConsoleOutput|null.
$this->assertOutputContains('Scanning of external links is not enabled');
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Utility/LinkScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit bd98375

Please sign in to comment.