Skip to content

Commit

Permalink
Merge pull request #54 from madewithlove/phpstan
Browse files Browse the repository at this point in the history
Bump PHPStan to max level
  • Loading branch information
WouterSioen committed Jun 3, 2021
2 parents d6fdfa1 + 5370659 commit 9c596ae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"license": "GPL-3.0-or-later",
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.0"
"phpunit/phpunit": "^8.5 || ^9.0",
"phpstan/phpstan": "^0.12.88"
}
}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 5
level: max
paths:
- src
- bin
3 changes: 3 additions & 0 deletions src/Htaccess/TableRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function (ResultLine $resultLine): array {
);
}

/**
* @param array<int, array{'url': string, 'output_url': string, 'status_code': ?int, 'expected url'?: string, 'matches'?: bool}> $results
*/
public function renderMultipleLineResult(array $results, SymfonyStyle $io): void
{
$hasExpectedUrl = !empty(array_filter(
Expand Down
48 changes: 30 additions & 18 deletions src/HtaccessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(HtaccessClient $htaccessClient, TableRenderer $table
$this->tableRenderer = $tableRenderer;
}

protected function configure()
protected function configure(): void
{
$this->addArgument('url', InputArgument::OPTIONAL, 'The request url to test your .htaccess file with');
$this->addOption('referrer', 'r', InputOption::VALUE_OPTIONAL, 'The referrer header, used as HTTP_REFERER in apache');
Expand All @@ -47,22 +47,28 @@ protected function configure()
$this->addOption('path', 'p', InputOption::VALUE_OPTIONAL, 'Path to the working directory you want to test in.');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$this->validateInput($input);

/** @var string $url */
$url = $input->getArgument('url');

/** @var string $path */
$path = $input->getOption('path') ? $input->getOption('path') : getcwd();

/** @var string $htaccess */
$htaccess = file_get_contents($path . '/.htaccess');

if ($url) {
return $this->testSingleUrl($url, $htaccess, $input, $io);
} else {
$urls = Yaml::parseFile($path . '/' . $input->getOption('url-list'));
/** @var string $urlListFile */
$urlListFile = $input->getOption('url-list');
/** @var string[] $urls */
$urls = Yaml::parseFile($path . '/' . $urlListFile);
$results = [];

foreach ($urls as $url => $expectedUrl) {
Expand Down Expand Up @@ -118,21 +124,24 @@ private function testSingleUrl(string $url, string $htaccess, InputInterface $in

if ($input->getOption('share')) {
try {
$share = $this->htaccessClient->share(
$url,
$htaccess,
$input->getOption('referrer'),
$input->getOption('server-name')
);
/** @var ?string $referrer */
$referrer = $input->getOption('referrer');
/** @var ?string $serverName */
$serverName = $input->getOption('server-name');

$share = $this->htaccessClient->share($url, $htaccess, $referrer, $serverName);

$io->text('You can share this test run on ' . $share->getShareUrl());
} catch (HtaccessException $exception) {
// when sharing failed, just ignore it
}
}

if ($input->getOption('expected-url') && $result->getOutputUrl() !== $input->getOption('expected-url')) {
$io->error('The output url is "' . $result->getOutputUrl() . '", while we expected "' . $input->getOption('expected-url') . '"');
/** @var ?string $expectedUrl */
$expectedUrl = $input->getOption('expected-url');

if ($expectedUrl && $result->getOutputUrl() !== $expectedUrl) {
$io->error('The output url is "' . $result->getOutputUrl() . '", while we expected "' . $expectedUrl . '"');

return 1;
}
Expand All @@ -142,10 +151,13 @@ private function testSingleUrl(string $url, string $htaccess, InputInterface $in
return 0;
}

private function validateInput(InputInterface $input)
private function validateInput(InputInterface $input): void
{
/** @var ?string $url */
$url = $input->getArgument('url');
/** @var ?string $urlList */
$urlList = $input->getOption('url-list');
/** @var ?string $path */
$path = $input->getOption('path') ? $input->getOption('path') : getcwd();


Expand Down Expand Up @@ -173,12 +185,12 @@ private function validateInput(InputInterface $input)

private function test(string $url, string $htaccess, InputInterface $input, ?SymfonyStyle $io = null): HtaccessResult
{
$result = $this->htaccessClient->test(
$url,
$htaccess,
$input->getOption('referrer'),
$input->getOption('server-name')
);
/** @var ?string $referrer */
$referrer = $input->getOption('referrer');
/** @var ?string $serverName */
$serverName = $input->getOption('server-name');

$result = $this->htaccessClient->test($url, $htaccess, $referrer, $serverName);

if ($io) {
$this->tableRenderer->renderHtaccessResult($result, $io);
Expand Down

0 comments on commit 9c596ae

Please sign in to comment.