Skip to content

Commit

Permalink
feat: Add PHPStan support
Browse files Browse the repository at this point in the history
  • Loading branch information
frankverhoeven committed May 30, 2022
1 parent 3541eba commit 1246605
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
"require-dev": {
"myonlinestore/coding-standard": "^3.1",
"phpunit/phpunit": "^8.5 || ^9.5",
"roave/infection-static-analysis-plugin": "^1.18",
"roave/infection-static-analysis-plugin": "^1.19",
"roave/security-advisories": "dev-latest",
"vimeo/psalm": "^4.22"
"vimeo/psalm": "^4.23",
"phpstan/phpstan": "^1.7",
"phpstan/phpstan-phpunit": "^1.1"
},
"bin": [
"bin/devtools"
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'
level: 9
paths:
- src
- tests
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
36 changes: 36 additions & 0 deletions src/Command/PhpStanCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;

final class PhpStanCommand extends DevToolsCommand
{
/** @var string|null */
protected static $defaultName = 'phpstan';

/** @var string|null */
protected static $defaultDescription = 'PHPStan';

protected function getProcess(InputInterface $input): Process
{
$command = [
$this->withVendorBinPath('phpstan'),
];

if ($this->isGitHubFormat($input)) {
$command[] = '--error-format=github';
}

return new Process($command, timeout: null);
}

public static function isAvailable(Configuration $configuration): bool
{
return \is_file($configuration->getRootDir() . 'phpstan.neon.dist')
|| \is_file($configuration->getRootDir() . 'phpstan.neon');
}
}
17 changes: 10 additions & 7 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,18 @@ private function determineThreads(): string
*/
private function gatherPhpVersions(): array
{
if (false === $composerJson = \file_get_contents($this->rootDir . 'composer.json')) {
throw new \RuntimeException('Unable to read "composer.json"');
}

/** @var array<array-key, mixed> $composer */
$composer = \json_decode(
\file_get_contents($this->rootDir . 'composer.json'),
true,
512,
\JSON_THROW_ON_ERROR
);
$composer = \json_decode($composerJson, true, 512, \JSON_THROW_ON_ERROR);

if (!isset($composer['require']['php'])) {
if (
!isset($composer['require'])
|| !\is_array($composer['require'])
|| !isset($composer['require']['php'])
) {
throw new \RuntimeException('Required PHP version not specified in composer.json');
}

Expand Down
2 changes: 2 additions & 0 deletions src/DevTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use MyOnlineStore\DevTools\Command\LintYamlCommand;
use MyOnlineStore\DevTools\Command\ListPhpVersionsCommand;
use MyOnlineStore\DevTools\Command\ListToolsCommand;
use MyOnlineStore\DevTools\Command\PhpStanCommand;
use MyOnlineStore\DevTools\Command\PhpUnitCommand;
use MyOnlineStore\DevTools\Command\PsalmCommand;
use MyOnlineStore\DevTools\Command\RoaveInfectionCommand;
Expand All @@ -35,6 +36,7 @@ public function getCommands(): array
new LintYamlCommand($this->configuration),
new ListToolsCommand($this->configuration),
new ListPhpVersionsCommand($this->configuration),
new PhpStanCommand($this->configuration),
new PhpUnitCommand($this->configuration),
new PsalmCommand($this->configuration),
new RoaveInfectionCommand($this->configuration),
Expand Down
3 changes: 3 additions & 0 deletions tests/DevToolsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use MyOnlineStore\DevTools\Command\LintYamlCommand;
use MyOnlineStore\DevTools\Command\ListPhpVersionsCommand;
use MyOnlineStore\DevTools\Command\ListToolsCommand;
use MyOnlineStore\DevTools\Command\PhpStanCommand;
use MyOnlineStore\DevTools\Command\PhpUnitCommand;
use MyOnlineStore\DevTools\Command\PsalmCommand;
use MyOnlineStore\DevTools\Command\RoaveInfectionCommand;
Expand Down Expand Up @@ -40,6 +41,7 @@ public function testGetCommands(): void
new LintYamlCommand($this->configuration),
new ListToolsCommand($this->configuration),
new ListPhpVersionsCommand($this->configuration),
new PhpStanCommand($this->configuration),
new PhpUnitCommand($this->configuration),
new PsalmCommand($this->configuration),
new RoaveInfectionCommand($this->configuration),
Expand All @@ -53,6 +55,7 @@ public function testListPhpVersion(): void
\chdir(\dirname(__DIR__));
$phpVersions = \exec('./bin/devtools list:php-versions');

self::assertIsString($phpVersions);
self::assertStringContainsString(\PHP_MAJOR_VERSION . '.' . \PHP_MINOR_VERSION, $phpVersions);
}
}

0 comments on commit 1246605

Please sign in to comment.