Skip to content

Commit ae7fd90

Browse files
authored
Command Refactoring (#143)
* Command Refactoring * Fix phpunit incompatibility
1 parent 03a7576 commit ae7fd90

File tree

10 files changed

+125
-158
lines changed

10 files changed

+125
-158
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"bin": ["bin/phpmnd"],
3838
"scripts": {
3939
"test": "phpunit",
40-
"cs-check": "phpcs -p --standard=PSR2 --runtime-set ignore_warnings_on_exit 1 src tests --ignore=tests/files",
41-
"cs-fix": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests --ignore=tests/files"
40+
"cs-check": "phpcs -p --standard=PSR2 --runtime-set ignore_warnings_on_exit 1 src tests --ignore=tests/Fixtures",
41+
"cs-fix": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests --ignore=tests/Fixtures"
4242
},
4343
"extra": {
4444
"branch-alias": {

phpunit.xml.dist

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit bootstrap="./vendor/autoload.php">
4-
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="./vendor/autoload.php"
5+
colors="true"
6+
executionOrder="random"
7+
failOnRisky="true"
8+
verbose="true"
9+
>
510
<testsuites>
611
<testsuite name="PHPMND Test Suite">
712
<directory>tests/</directory>
13+
14+
<exclude>tests/Fixtures</exclude>
815
</testsuite>
916
</testsuites>
1017

1118
<filter>
1219
<whitelist>
1320
<directory>src/</directory>
21+
1422
<exclude>
1523
<directory>vendor/</directory>
1624
</exclude>
1725
</whitelist>
1826
</filter>
19-
20-
</phpunit>
27+
</phpunit>

src/Console/Command.php renamed to src/Command/RunCommand.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace Povils\PHPMND\Console;
5+
namespace Povils\PHPMND\Command;
66

7+
use Povils\PHPMND\Console\Option;
78
use Povils\PHPMND\Detector;
89
use Povils\PHPMND\ExtensionResolver;
910
use Povils\PHPMND\FileReportList;
@@ -19,10 +20,15 @@
1920
use Symfony\Component\Console\Input\InputOption;
2021
use Symfony\Component\Console\Output\OutputInterface;
2122

22-
class Command extends BaseCommand
23+
/**
24+
* Class Command
25+
*
26+
* @package Povils\PHPMND\Console
27+
*/
28+
class RunCommand extends BaseCommand
2329
{
24-
const EXIT_CODE_SUCCESS = 0;
25-
const EXIT_CODE_FAILURE = 1;
30+
public const SUCCESS = 0;
31+
public const FAILURE = 1;
2632

2733
/**
2834
* @var Timer
@@ -32,15 +38,11 @@ class Command extends BaseCommand
3238
protected function configure(): void
3339
{
3440
$this
35-
->setName('phpmnd')
36-
->setDefinition(
37-
[
38-
new InputArgument(
39-
'directories',
40-
InputArgument::REQUIRED + InputArgument::IS_ARRAY,
41-
'One or more files and/or directories to analyze'
42-
),
43-
]
41+
->setName('run')
42+
->addArgument(
43+
'directories',
44+
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
45+
'One or more files and/or directories to analyze'
4446
)
4547
->addOption(
4648
'extensions',
@@ -151,7 +153,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
151153

152154
if (0 === $finder->count()) {
153155
$output->writeln('No files found to scan');
154-
return self::EXIT_CODE_SUCCESS;
156+
return self::SUCCESS;
155157
}
156158

157159
$progressBar = null;
@@ -160,11 +162,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
160162
$progressBar->start();
161163
}
162164

163-
$hintList = new HintList;
165+
$hintList = new HintList();
164166
$detector = new Detector($this->createOption($input), $hintList);
165167

166168
$fileReportList = new FileReportList();
167-
$printer = new Printer\Console();
168169
$whitelist = $this->getFileOption($input->getOption('whitelist'));
169170

170171
foreach ($finder as $file) {
@@ -195,21 +196,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
195196
$xmlOutput->printData($output, $fileReportList, $hintList);
196197
}
197198

198-
if ($output->getVerbosity() !== OutputInterface::VERBOSITY_QUIET) {
199+
if (!$output->isQuiet()) {
199200
$output->writeln('');
201+
$printer = new Printer\Console();
200202
$printer->printData($output, $fileReportList, $hintList);
201203
$output->writeln('<info>' . $this->getResourceUsage() . '</info>');
202204
}
203205

204206
if ($input->getOption('non-zero-exit-on-violation') && $fileReportList->hasMagicNumbers()) {
205-
return self::EXIT_CODE_FAILURE;
207+
return self::FAILURE;
206208
}
207-
return self::EXIT_CODE_SUCCESS;
209+
210+
return self::SUCCESS;
208211
}
209212

210213
private function createOption(InputInterface $input): Option
211214
{
212-
$option = new Option;
215+
$option = new Option();
213216
$option->setIgnoreNumbers(array_map([$this, 'castToNumber'], $this->getCSVOption($input, 'ignore-numbers')));
214217
$option->setIgnoreFuncs($this->getCSVOption($input, 'ignore-funcs'));
215218
$option->setIncludeStrings($input->getOption('strings'));

src/Console/Application.php

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,52 @@
44

55
namespace Povils\PHPMND\Console;
66

7+
use Povils\PHPMND\Command\RunCommand;
78
use Symfony\Component\Console\Application as BaseApplication;
9+
use Symfony\Component\Console\Command\HelpCommand;
810
use Symfony\Component\Console\Input\ArrayInput;
9-
use Symfony\Component\Console\Input\InputDefinition;
1011
use Symfony\Component\Console\Input\InputInterface;
1112
use Symfony\Component\Console\Output\OutputInterface;
1213

1314
class Application extends BaseApplication
1415
{
15-
const VERSION = '2.4.0';
16-
const COMMAND_NAME = 'phpmnd';
16+
public const VERSION = '2.4.0';
17+
private const NAME = 'phpmnd';
1718

1819
public function __construct()
1920
{
20-
parent::__construct('phpmnd', self::VERSION);
21-
}
22-
23-
protected function getCommandName(InputInterface $input): string
24-
{
25-
return self::COMMAND_NAME;
26-
}
27-
28-
protected function getDefaultCommands(): array
29-
{
30-
$defaultCommands = parent::getDefaultCommands();
31-
$defaultCommands[] = new Command;
21+
parent::__construct(self::NAME, self::VERSION);
3222

33-
return $defaultCommands;
34-
}
35-
36-
public function getDefinition(): InputDefinition
37-
{
38-
$inputDefinition = parent::getDefinition();
39-
$inputDefinition->setArguments();
40-
41-
return $inputDefinition;
23+
$this->setDefaultCommand('run', true);
4224
}
4325

4426
public function doRun(InputInterface $input, OutputInterface $output): int
4527
{
46-
if (false === $input->hasParameterOption('--quiet')) {
47-
$output->write(
48-
sprintf(
49-
'phpmnd %s by Povilas Susinskas' . PHP_EOL,
50-
$this->getVersion()
51-
)
52-
);
53-
}
28+
$hasVersionOption = $input->hasParameterOption(['--version', '-V'], true);
5429

55-
if ($input->hasParameterOption('--version') || $input->hasParameterOption('-V')) {
56-
return Command::EXIT_CODE_SUCCESS;
30+
if ($hasVersionOption === false) {
31+
$output->writeln($this->getLongVersion());
32+
$output->writeln('');
5733
}
5834

59-
if (null === $input->getFirstArgument()) {
35+
if ($hasVersionOption === false && $input->getFirstArgument() === null) {
6036
$input = new ArrayInput(['--help']);
6137
}
6238

6339
return parent::doRun($input, $output);
6440
}
41+
42+
public function getLongVersion(): string
43+
{
44+
return trim(sprintf(
45+
'<info>%s</info> version <comment>%s</comment> by Povilas Susinskas',
46+
$this->getName(),
47+
$this->getVersion()
48+
));
49+
}
50+
51+
protected function getDefaultCommands(): array
52+
{
53+
return [new HelpCommand(), new RunCommand()];
54+
}
6555
}

tests/Command/RunCommandTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Povils\PHPMND\Tests\Command;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Povils\PHPMND\Command\RunCommand;
9+
use Povils\PHPMND\Console\Application;
10+
use Symfony\Component\Console\Tester\CommandTester;
11+
12+
class RunCommandTest extends TestCase
13+
{
14+
/**
15+
* @var CommandTester
16+
*/
17+
private $commandTester;
18+
19+
protected function setUp(): void
20+
{
21+
$application = new Application();
22+
$command = new RunCommand();
23+
$application->add($command);
24+
25+
$this->commandTester = new CommandTester($application->find($command->getName()));
26+
}
27+
28+
public function testExecuteNoFilesFound(): void
29+
{
30+
$this->commandTester->execute([
31+
'directories' => ['tests/Fixtures'],
32+
'--suffixes' => 'bad_suffix',
33+
]);
34+
35+
$this->assertSame(RunCommand::SUCCESS, $this->commandTester->getStatusCode());
36+
}
37+
38+
public function testExecuteWithViolationOption(): void
39+
{
40+
$this->commandTester->execute([
41+
'directories' => ['tests/Fixtures'],
42+
'--non-zero-exit-on-violation' => true,
43+
]);
44+
45+
$this->assertSame(RunCommand::FAILURE, $this->commandTester->getStatusCode());
46+
}
47+
48+
public function testExecuteWithHintOption(): void
49+
{
50+
$this->commandTester->execute([
51+
'directories' => ['tests/Fixtures'],
52+
'--extensions' => 'assign',
53+
'--non-zero-exit-on-violation' => true,
54+
'--hint' => true,
55+
]);
56+
57+
$this->assertSame(RunCommand::FAILURE, $this->commandTester->getStatusCode());
58+
$this->assertTrue(strpos($this->commandTester->getDisplay(), 'Suggestions:') !== false);
59+
}
60+
}

tests/Console/CommandTest.php

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)