Skip to content

Commit

Permalink
[minor] add static code analysis with phpstan (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Feb 14, 2022
1 parent ef51134 commit dad0fae
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 13 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ jobs:

cs-check:
uses: zenstruck/.github/.github/workflows/php-cs-fixer.yml@main

sca:
name: Static Code Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: none

- name: Install Dependencies
uses: ramsey/composer-install@v1

- name: Run PHPStan
run: vendor/bin/phpstan --error-format=github
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"zenstruck/assert": "^1.0"
},
"require-dev": {
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/phpunit-bridge": "^5.3"
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
paths:
- src
- tests
3 changes: 3 additions & 0 deletions src/InteractsWithConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
trait InteractsWithConsole
{
/**
* @param string[] $inputs
*/
final protected function executeConsoleCommand(string $command, array $inputs = []): CommandResult
{
return $this->consoleCommand($command)
Expand Down
13 changes: 9 additions & 4 deletions src/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ final class TestCommand
{
private Application $application;
private string $cli;

/** @var string[] */
private array $inputs = [];
private bool $splitOutputStreams = false;

private function __construct(Command $command, string $cli)
{
if (!$command->getApplication()) {
if (!$application = $command->getApplication()) {
$application = new Application();
$application->add($command);

$command->setApplication($application);
}

$this->application = $command->getApplication();
$this->application = $application;
$this->cli = $cli;
}

public static function for(Command $command): self
{
return new self($command, $command->getName());
return new self($command, (string) $command->getName());
}

public static function from(Application $application, string $cli): self
Expand Down Expand Up @@ -59,7 +61,7 @@ public function addArgument(string $value): self
}

/**
* @param string|array|null $value
* @param string|string[]|null $value
*/
public function addOption(string $name, $value = null): self
{
Expand All @@ -84,6 +86,9 @@ public function addInput(string $value): self
return $this;
}

/**
* @param string[] $inputs
*/
public function withInput(array $inputs): self
{
$this->inputs = $inputs;
Expand Down
10 changes: 9 additions & 1 deletion src/TestInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
*/
final class TestInput extends StringInput
{
/**
* @param string[] $inputs
*/
public function __construct(string $input, array $inputs)
{
parent::__construct($input);

parent::setInteractive(false);

if ($inputs) {
$stream = \fopen('php://memory', 'r+', false);
if (!$stream = \fopen('php://memory', 'r+', false)) {
throw new \RuntimeException('Failed to open stream.');
}

foreach ($inputs as $value) {
\fwrite($stream, $value.\PHP_EOL);
Expand All @@ -36,6 +41,9 @@ public function __construct(string $input, array $inputs)
}
}

/**
* @param bool $interactive
*/
public function setInteractive($interactive): void
{
// noop, prevent Application from setting this value
Expand Down
36 changes: 28 additions & 8 deletions src/TestOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
final class TestOutput extends StreamOutput implements ConsoleOutputInterface
{
private ?OutputInterface $error = null;

/** @var ConsoleSectionOutput[] */
private array $sections = [];

public function __construct(bool $splitStreams, TestInput $input)
{
parent::__construct(
\fopen('php://memory', 'w', false),
$input->getVerbosity(),
$input->isDecorated()
);
if (!$stream = \fopen('php://memory', 'w', false)) {
throw new \RuntimeException('Failed to open stream.');
}

parent::__construct($stream, $input->getVerbosity(), $input->isDecorated());

if ($splitStreams) {
$this->error = new StreamOutput(\fopen('php://memory', 'w', false));
if (!$stream = \fopen('php://memory', 'w', false)) {
throw new \RuntimeException('Failed to open stream.');
}

$this->error = new StreamOutput($stream);
$this->error->setFormatter($this->getFormatter());
$this->error->setVerbosity($this->getVerbosity());
$this->error->setDecorated($this->isDecorated());
Expand All @@ -48,11 +54,17 @@ public function section(): ConsoleSectionOutput
return new ConsoleSectionOutput($this->getStream(), $this->sections, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
}

/**
* @param bool $decorated
*/
public function setDecorated($decorated): void
{
// noop, prevent Application from setting this value
}

/**
* @param int $level
*/
public function setVerbosity($level): void
{
// noop, prevent Application from setting this value
Expand All @@ -62,7 +74,11 @@ public function getDisplay(): string
{
\rewind($this->getStream());

return \stream_get_contents($this->getStream());
if (false === $contents = \stream_get_contents($this->getStream())) {
throw new \RuntimeException('Failed to read stream.');
}

return $contents;
}

public function getErrorDisplay(): string
Expand All @@ -73,6 +89,10 @@ public function getErrorDisplay(): string

\rewind($this->error->getStream());

return \stream_get_contents($this->error->getStream());
if (false === $contents = \stream_get_contents($this->error->getStream())) {
throw new \RuntimeException('Failed to read stream.');
}

return $contents;
}
}
4 changes: 4 additions & 0 deletions tests/Fixture/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand All @@ -31,6 +32,9 @@ protected function configure(): void
;
}

/**
* @param ConsoleOutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$errOutput = $output->getErrorOutput();
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
]);
}

/**
* BC Layer.
*
* @param mixed $routes
*/
protected function configureRoutes($routes): void
{
// noop
Expand Down

0 comments on commit dad0fae

Please sign in to comment.