Skip to content

Commit

Permalink
bump dependencies and adopt Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
holtkamp committed Jan 22, 2024
1 parent f03eb1c commit 80fe908
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 181 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ phpstan:
phpunit:
vendor/bin/phpunit

rector:
./vendor/bin/rector process

test: phpstan phpunit

verifyCode: rector phpcs-fix phpstan
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
"description": "PHP Sitemap crawler",
"license": "MIT",
"require": {
"php": "^8.0",
"php": "~8.3",
"ext-simplexml": "*",
"clue/reactphp-flux": "^1.3",
"psr/log": "^1.1 || ^2.0",
"clue/reactphp-flux": "^1.4",
"psr/log": "^2.0 || ^3.0",
"react/filesystem": "^0.1",
"react/http": "^1.4",
"symfony/console": "^5.2 || ^6.0 || ^7.0",
"react/http": "^1.9",
"symfony/console": "^6.0 || ^7.0",
"teapot/status-code": "^2.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "@stable",
"phpstan/phpstan": "@stable",
"phpunit/phpunit": "@stable",
"rector/rector": "@stable",
"roave/security-advisories": "dev-master"
},
"config": {
Expand Down
38 changes: 38 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Assign\NullCoalescingOperatorRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector;
use Rector\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rule(StaticArrowFunctionRector::class);
$rectorConfig->rule(NullCoalescingOperatorRector::class);
$rectorConfig->rule(ClosureToArrowFunctionRector::class);
$rectorConfig->rule(ArrayKeyExistsOnPropertyRector::class);
$rectorConfig->rule(ParenthesizeNestedTernaryRector::class);
$rectorConfig->rule(FirstClassCallableRector::class);
$rectorConfig->rule(FinalizePublicClassConstantRector::class);
$rectorConfig->rule(ReadOnlyPropertyRector::class);
$rectorConfig->rule(AddTypeToConstRector::class);
$rectorConfig->rule(AddArrowFunctionReturnTypeRector::class);
$rectorConfig->rule(AddMethodCallBasedStrictParamTypeRector::class); //Seems to not consider re-using imported types / aliases

$rectorConfig->paths([
__DIR__ . '/src',
]);

$rectorConfig->phpVersion(PhpVersion::PHP_83);
$rectorConfig->parallel(600);
};
78 changes: 17 additions & 61 deletions src/Command/RunOctopusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Octopus\Command;

use DateTime;
use Octopus\Config;
use Octopus\Presenter\TablePresenter;
use Octopus\Processor;
Expand All @@ -22,63 +21,20 @@

class RunOctopusCommand extends Command
{
/**
* @var string
*/
private const COMMAND_ARGUMENT_SITEMAP_FILE = 'sitemap';

/**
* @var string
*/
private const COMMAND_OPTION_ADDITIONAL_RESPONSE_HEADERS_TO_COUNT = 'additionalResponseHeadersToCount';

/**
* @var string
*/
private const COMMAND_OPTION_CONCURRENCY = 'concurrency';

/**
* @var string
*/
private const COMMAND_OPTION_PRESENTER = 'presenter';

/**
* @var string
*/
private const COMMAND_OPTION_TIMEOUT = 'timeout';

/**
* @var string
*/
private const COMMAND_OPTION_TIMER_UI = 'timerUI';

/**
* @var string
*/
private const COMMAND_OPTION_FOLLOW_HTTP_REDIRECTS = 'followRedirects';

/**
* @var string
*/
private const COMMAND_OPTION_USER_AGENT = 'userAgent';

/**
* @var string
*/
private const COMMAND_OPTION_REQUEST_TYPE = 'requestType';

/**
* @var string
*/
private const COMMAND_OPTION_TARGET_TYPE = 'targetFormat';

/**
* @var string
*/
private const DATE_FORMAT = DateTime::W3C;

private DateTime $crawlingStartedDateTime;
private DateTime $crawlingEndedDateTime;
private const string COMMAND_ARGUMENT_SITEMAP_FILE = 'sitemap';
private const string COMMAND_OPTION_ADDITIONAL_RESPONSE_HEADERS_TO_COUNT = 'additionalResponseHeadersToCount';
private const string COMMAND_OPTION_CONCURRENCY = 'concurrency';
private const string COMMAND_OPTION_PRESENTER = 'presenter';
private const string COMMAND_OPTION_TIMEOUT = 'timeout';
private const string COMMAND_OPTION_TIMER_UI = 'timerUI';
private const string COMMAND_OPTION_FOLLOW_HTTP_REDIRECTS = 'followRedirects';
private const string COMMAND_OPTION_USER_AGENT = 'userAgent';
private const string COMMAND_OPTION_REQUEST_TYPE = 'requestType';
private const string COMMAND_OPTION_TARGET_TYPE = 'targetFormat';
private const string DATE_FORMAT = \DateTimeImmutable::W3C;

private \DateTimeImmutable $crawlingStartedDateTime;
private \DateTimeImmutable $crawlingEndedDateTime;
private InputInterface $input;
private LoggerInterface $logger;
private ConsoleOutputInterface $output;
Expand Down Expand Up @@ -118,14 +74,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
\assert($output instanceof ConsoleOutputInterface);
$this->input = $input;
$this->output = $output;
$this->crawlingStartedDateTime = new DateTime();
$this->crawlingStartedDateTime = new \DateTimeImmutable();
$this->getLogger()->debug('Starting Octopus Sitemap Crawler');

$config = $this->determineConfiguration();
$processor = new Processor($config, $this->getLogger());
$processor->run();

$this->crawlingEndedDateTime = new DateTime();
$this->crawlingEndedDateTime = new \DateTimeImmutable();

$this->renderResultsTable($processor);

Expand All @@ -152,7 +108,7 @@ private function determineConfiguration(): Config
if (\is_string($this->input->getOption(self::COMMAND_OPTION_ADDITIONAL_RESPONSE_HEADERS_TO_COUNT))) {
$additionalResponseHeadersToCount = $this->input->getOption(self::COMMAND_OPTION_ADDITIONAL_RESPONSE_HEADERS_TO_COUNT);
$config->additionalResponseHeadersToCount = explode(',', $additionalResponseHeadersToCount);
$this->getLogger()->notice('keep track of "{additionalResponseHeadersToCountNumber}" additional response headers: "{additionalResponseHeadersToCount}"', ['additionalResponseHeadersToCountNumber' => count($config->additionalResponseHeadersToCount), 'additionalResponseHeadersToCount' => implode(', ', $config->additionalResponseHeadersToCount)]);
$this->getLogger()->notice('keep track of "{additionalResponseHeadersToCountNumber}" additional response headers: "{additionalResponseHeadersToCount}"', ['additionalResponseHeadersToCountNumber' => \count($config->additionalResponseHeadersToCount), 'additionalResponseHeadersToCount' => implode(', ', $config->additionalResponseHeadersToCount)]);
}
if (is_numeric($this->input->getOption(self::COMMAND_OPTION_CONCURRENCY))) {
$config->concurrency = (int) $this->input->getOption(self::COMMAND_OPTION_CONCURRENCY);
Expand Down
89 changes: 15 additions & 74 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,80 +12,21 @@
*/
class Config
{
/**
* @var bool
*/
public const FOLLOW_HTTP_REDIRECTS_DEFAULT = true;

/**
* @var string
*/
public const OUTPUT_MODE_COUNT = 'count';

/**
* @var string
*/
public const OUTPUT_MODE_SAVE = 'save';

/**
* @var string
*/
public const PRESENTER_DEFAULT = EchoPresenter::class;

/**
* @var string
*/
public const REQUEST_HEADER_USER_AGENT = 'User-Agent';

/**
* @var string
*/
public const REQUEST_HEADER_USER_AGENT_DEFAULT = 'Octopus/1.0';

/**
* @var string
*/
public const REQUEST_TYPE_GET = 'GET';

/**
* @var string
*/
public const REQUEST_TYPE_HEAD = 'HEAD';

/**
* @var string
*/
public const REQUEST_TYPE_DEFAULT = self::REQUEST_TYPE_HEAD;

/**
* @var string
*/
public const TARGET_TYPE_XML = 'xml';

/**
* @var string
*/
public const TARGET_TYPE_TXT = 'txt';

/**
* @var string
*/
public const TARGET_TYPE_DEFAULT = self::TARGET_TYPE_XML;

/**
* @var int
*/
public const CONCURRENCY_DEFAULT = 5;

/**
* @var float
*/
public const TIMEOUT_DEFAULT = 10.0;

/**
* @var float
*/
public const TIMER_UI_DEFAULT = 0.25;
final public const bool FOLLOW_HTTP_REDIRECTS_DEFAULT = true;
final public const string OUTPUT_MODE_COUNT = 'count';
final public const string OUTPUT_MODE_SAVE = 'save';
final public const string PRESENTER_DEFAULT = EchoPresenter::class;
final public const string REQUEST_HEADER_USER_AGENT = 'User-Agent';
final public const string REQUEST_HEADER_USER_AGENT_DEFAULT = 'Octopus/1.0';
final public const string REQUEST_TYPE_GET = 'GET';
final public const string REQUEST_TYPE_HEAD = 'HEAD';
final public const string REQUEST_TYPE_DEFAULT = self::REQUEST_TYPE_HEAD;
final public const string TARGET_TYPE_XML = 'xml';
final public const string TARGET_TYPE_TXT = 'txt';
final public const string TARGET_TYPE_DEFAULT = self::TARGET_TYPE_XML;
final public const int CONCURRENCY_DEFAULT = 5;
final public const float TIMEOUT_DEFAULT = 10.0;
final public const float TIMER_UI_DEFAULT = 0.25;

/**
* An array of some additional response headers to count.
Expand Down
2 changes: 1 addition & 1 deletion src/Presenter/TablePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class TablePresenter implements Presenter
{
private ConsoleOutputInterface $output;
private readonly ConsoleOutputInterface $output;

/**
* @var Table[]
Expand Down
33 changes: 13 additions & 20 deletions src/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;
use React\Promise\Timer\TimeoutException;
use RuntimeException;
use Teapot\StatusCode\Http;

/**
Expand All @@ -28,17 +27,11 @@ class Processor
{
/**
* Used for indicating a general error when no more details can be provided.
*
* @var string
*/
private const ERROR_TYPE_GENERAL = 'failure';

/**
* @var string
*/
private const ERROR_TYPE_TIMEOUT = 'timeout';
private const string ERROR_TYPE_GENERAL = 'failure';
private const string ERROR_TYPE_TIMEOUT = 'timeout';

public Config$config;
public Config $config;
public Result $result;

/**
Expand All @@ -53,7 +46,7 @@ class Processor
];

private Browser $browser;
private LoggerInterface $logger;
private readonly LoggerInterface $logger;
private StreamTargetManager $targetManager;
private Transformer $transformer;
private bool $followRedirects = false;
Expand Down Expand Up @@ -131,7 +124,7 @@ private function touchOutputDestination(): void
{
$savePath = $this->config->outputDestination.\DIRECTORY_SEPARATOR;
if (!@mkdir($savePath) && !is_dir($savePath)) {
throw new Exception('Cannot create output directory: '.$savePath);
throw new \Exception('Cannot create output directory: '.$savePath);
}
}

Expand Down Expand Up @@ -326,7 +319,7 @@ private function getLocationFromHeaders(array $headers): string
*/
private function getOnRejectedCallback(string $url): callable
{
return function (array|Exception $errorOrException) use ($url): void {
return function (array|\Exception $errorOrException) use ($url): void {
$errorType = $this->getErrorType($errorOrException);
$this->result->done($url);
$this->result->addBrokenUrl($url, $errorType);
Expand All @@ -340,9 +333,9 @@ private function getOnRejectedCallback(string $url): callable
}

/**
* @param array<int, string>|Exception $errorOrException
* @param array<int, string>|\Exception $errorOrException
*/
private function getErrorType(array|Exception $errorOrException): string
private function getErrorType(array|\Exception $errorOrException): string
{
if ($errorOrException instanceof TimeoutException) {
return self::ERROR_TYPE_TIMEOUT;
Expand All @@ -353,18 +346,18 @@ private function getErrorType(array|Exception $errorOrException): string
}

// This could help to distinguish UnexpectedValueException, InvalidArgumentException, etc.
if ($errorOrException instanceof RuntimeException) {
return \get_class($errorOrException);
if ($errorOrException instanceof \RuntimeException) {
return $errorOrException::class;
}

return self::ERROR_TYPE_GENERAL;
}

/**
* @param array<int, string>|Exception $errorOrException
* @param array<int, string>|\Exception $errorOrException
*/
private function getErrorMessage(array|Exception $errorOrException): string
private function getErrorMessage(array|\Exception $errorOrException): string
{
return $errorOrException instanceof Exception ? $errorOrException->getMessage() : print_r($errorOrException, true);
return $errorOrException instanceof \Exception ? $errorOrException->getMessage() : print_r($errorOrException, true);
}
}
Loading

0 comments on commit 80fe908

Please sign in to comment.