Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit b175242

Browse files
authored
Merge pull request #117 from MarvinKlemp/feature/simple_output
Simple Console output
2 parents 7707685 + de6809c commit b175242

File tree

10 files changed

+162
-57
lines changed

10 files changed

+162
-57
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ You can get a list of all options and arguments by running
9797
$ deprecation-detector check --help
9898
```
9999

100+
The default output might not fit into the cli. If that is the case you can set the output to a list by setting `--output=simple`.
101+
102+
```bash
103+
$ deprecation-detector check src/ vendor/ --output=simple
104+
```
105+
100106
## Excluding deprecated method calls
101107

102108
You can exclude deprecated method calls by using the `filter-methods` option. This option takes a comma separated list of method references to exclude.

src/Configuration/Configuration.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class Configuration
4444
*/
4545
private $logHtml;
4646

47+
/**
48+
* @var string
49+
*/
50+
private $output;
51+
4752
/**
4853
* @param string $ruleSet
4954
* @param string $containerPath
@@ -53,6 +58,7 @@ class Configuration
5358
* @param bool $fail
5459
* @param bool $verbose
5560
* @param string $logHtml
61+
* @param string $output
5662
*/
5763
public function __construct(
5864
$ruleSet,
@@ -62,7 +68,8 @@ public function __construct(
6268
$filterMethodCalls,
6369
$fail,
6470
$verbose,
65-
$logHtml)
71+
$logHtml,
72+
$output)
6673
{
6774
$this->ruleSet = $ruleSet;
6875
$this->containerPath = $containerPath;
@@ -72,6 +79,7 @@ public function __construct(
7279
$this->failOnDeprecation = $fail;
7380
$this->verbose = $verbose;
7481
$this->logHtml = $logHtml;
82+
$this->output = $output;
7583
}
7684

7785
public function overrideConfiguration()
@@ -138,4 +146,12 @@ public function logHtml()
138146
{
139147
return $this->logHtml;
140148
}
149+
150+
/**
151+
* @return bool
152+
*/
153+
public function isSimpleOutput()
154+
{
155+
return $this->output === 'simple';
156+
}
141157
}

src/Console/Command/CheckCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function configure()
3838
new InputOption('no-cache', null, InputOption::VALUE_NONE, 'Disable rule set cache'),
3939
new InputOption('cache-dir', null, InputOption::VALUE_REQUIRED, 'Cache directory', '.rules/'),
4040
new InputOption('log-html', null, InputOption::VALUE_REQUIRED, 'Generate HTML report'),
41+
new InputOption('output', null, InputOption::VALUE_REQUIRED, 'Change the output mode'),
4142
new InputOption('filter-methods', null, InputOption::VALUE_OPTIONAL, 'Filter methods', ''),
4243
new InputOption('fail', null, InputOption::VALUE_NONE, 'Fails, if any deprecation is detected'),
4344
)
@@ -113,7 +114,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
113114
$input->getOption('filter-methods'),
114115
$input->getOption('fail'),
115116
$input->getOption('verbose'),
116-
$input->getOption('log-html')
117+
$input->getOption('log-html'),
118+
$input->getOption('output')
117119
);
118120

119121
$factory = new DetectorFactory();

src/DetectorFactory.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
use SensioLabs\DeprecationDetector\TypeGuessing\SymbolTable\SymbolTable;
3636
use SensioLabs\DeprecationDetector\TypeGuessing\SymbolTable\Visitor\SymbolTableVariableResolverVisitor;
3737
use SensioLabs\DeprecationDetector\TypeGuessing\Symfony\ContainerReader;
38-
use SensioLabs\DeprecationDetector\Violation\Renderer\ConsoleOutputRenderer;
38+
use SensioLabs\DeprecationDetector\Violation\Renderer\Console\DefaultRenderer;
3939
use SensioLabs\DeprecationDetector\Violation\Renderer\Html\RendererFactory;
4040
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\Message\ClassViolationMessage;
4141
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\Message\FunctionViolationMessage;
@@ -45,6 +45,7 @@
4545
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\Message\MethodViolationMessage;
4646
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\Message\SuperTypeViolationMessage;
4747
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper;
48+
use SensioLabs\DeprecationDetector\Violation\Renderer\Console\SimpleRenderer;
4849
use SensioLabs\DeprecationDetector\Violation\ViolationChecker\ClassViolationChecker;
4950
use SensioLabs\DeprecationDetector\Violation\ViolationChecker\ComposedViolationChecker;
5051
use SensioLabs\DeprecationDetector\Violation\ViolationChecker\FunctionViolationChecker;
@@ -293,7 +294,7 @@ private function getViolationFilter(Configuration $configuration)
293294
* @param Configuration $configuration
294295
* @param OutputInterface $output
295296
*
296-
* @return ConsoleOutputRenderer|Violation\Renderer\Html\Renderer
297+
* @return DefaultRenderer|Violation\Renderer\Html\Renderer
297298
*/
298299
private function getRenderer(Configuration $configuration, OutputInterface $output)
299300
{
@@ -305,7 +306,11 @@ private function getRenderer(Configuration $configuration, OutputInterface $outp
305306
return $factory->createHtmlOutputRenderer($logFilePath);
306307
}
307308

308-
return new ConsoleOutputRenderer($output, $messageHelper);
309+
if ($configuration->isSimpleOutput()) {
310+
return new SimpleRenderer($output, $messageHelper);
311+
}
312+
313+
return new DefaultRenderer($output, $messageHelper);
309314
}
310315

311316
/**

src/FileInfo/PhpFileInfo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function hasClassDeprecation($className)
280280
*/
281281
public function getClassDeprecation($className)
282282
{
283-
return ($this->hasClassDeprecation($className) ? $this->classDeprecations[$className] : null);
283+
return $this->hasClassDeprecation($className) ? $this->classDeprecations[$className] : null;
284284
}
285285

286286
/**
@@ -324,7 +324,7 @@ public function hasInterfaceDeprecation($interfaceName)
324324
*/
325325
public function getInterfaceDeprecation($interfaceName)
326326
{
327-
return ($this->hasInterfaceDeprecation($interfaceName) ? $this->interfaceDeprecations[$interfaceName] : null);
327+
return $this->hasInterfaceDeprecation($interfaceName) ? $this->interfaceDeprecations[$interfaceName] : null;
328328
}
329329

330330
/**

src/TypeGuessing/SymbolTable/Resolver/SymfonyResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,6 @@ protected function getType($context, $methodName, Node $node = null)
121121
*/
122122
protected function isController($type)
123123
{
124-
return (substr($type, -10) === 'Controller');
124+
return substr($type, -10) === 'Controller';
125125
}
126126
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace SensioLabs\DeprecationDetector\Violation\Renderer\Console;
4+
5+
use PhpParser\Error;
6+
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper;
7+
use SensioLabs\DeprecationDetector\Violation\Renderer\RendererInterface;
8+
use SensioLabs\DeprecationDetector\Violation\Violation;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
abstract class BaseRenderer implements RendererInterface
12+
{
13+
/**
14+
* @var OutputInterface
15+
*/
16+
protected $output;
17+
18+
/**
19+
* @var MessageHelper
20+
*/
21+
protected $messageHelper;
22+
23+
/**
24+
* @param OutputInterface $output
25+
* @param MessageHelper $messageHelper
26+
*/
27+
public function __construct(OutputInterface $output, MessageHelper $messageHelper)
28+
{
29+
$this->output = $output;
30+
$this->messageHelper = $messageHelper;
31+
}
32+
33+
/**
34+
* @param Violation[] $violations
35+
* @param Error[] $errors
36+
*/
37+
public function renderViolations(array $violations, array $errors)
38+
{
39+
$this->printViolations($violations);
40+
$this->printErrors($errors);
41+
}
42+
43+
/**
44+
* @param Violation[] $violations
45+
*/
46+
abstract protected function printViolations(array $violations);
47+
48+
/**
49+
* @param Error[] $errors
50+
*/
51+
protected function printErrors(array $errors)
52+
{
53+
if (0 === count($errors)) {
54+
return;
55+
}
56+
57+
$this->output->writeln('');
58+
$this->output->writeln('<error>Your project contains invalid code:</error>');
59+
foreach ($errors as $error) {
60+
$this->output->writeln(
61+
sprintf(
62+
'<error>%s</error>',
63+
$error->getRawMessage()
64+
)
65+
);
66+
}
67+
}
68+
}

src/Violation/Renderer/ConsoleOutputRenderer.php renamed to src/Violation/Renderer/Console/DefaultRenderer.php

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

3-
namespace SensioLabs\DeprecationDetector\Violation\Renderer;
3+
namespace SensioLabs\DeprecationDetector\Violation\Renderer\Console;
44

5-
use PhpParser\Error;
65
use SensioLabs\DeprecationDetector\FileInfo\PhpFileInfo;
76
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper;
87
use SensioLabs\DeprecationDetector\Violation\Violation;
@@ -11,42 +10,21 @@
1110
use Symfony\Component\Console\Helper\TableSeparator;
1211
use Symfony\Component\Console\Output\OutputInterface;
1312

14-
class ConsoleOutputRenderer implements RendererInterface
13+
class DefaultRenderer extends BaseRenderer
1514
{
16-
/**
17-
* @var OutputInterface
18-
*/
19-
protected $output;
20-
21-
/**
22-
* @var MessageHelper
23-
*/
24-
private $messageHelper;
25-
2615
/**
2716
* @param OutputInterface $output
2817
* @param MessageHelper $messageHelper
2918
*/
3019
public function __construct(OutputInterface $output, MessageHelper $messageHelper)
3120
{
32-
$this->output = $output;
33-
$this->messageHelper = $messageHelper;
21+
parent::__construct($output, $messageHelper);
3422
}
3523

3624
/**
3725
* @param Violation[] $violations
38-
* @param Error[] $errors
3926
*/
40-
public function renderViolations(array $violations, array $errors)
41-
{
42-
$this->printViolations($violations);
43-
$this->printErrors($errors);
44-
}
45-
46-
/**
47-
* @param Violation[] $violations
48-
*/
49-
private function printViolations(array $violations)
27+
protected function printViolations(array $violations)
5028
{
5129
if (0 === count($violations)) {
5230
return;
@@ -77,26 +55,6 @@ private function printViolations(array $violations)
7755
$table->render();
7856
}
7957

80-
/**
81-
* @param Error[] $errors
82-
*/
83-
private function printErrors(array $errors)
84-
{
85-
if (0 === count($errors)) {
86-
return;
87-
}
88-
89-
$this->output->writeln('<error>Your project contains invalid code:</error>');
90-
foreach ($errors as $error) {
91-
$this->output->writeln(
92-
sprintf(
93-
'<error>%s</error>',
94-
$error->getRawMessage()
95-
)
96-
);
97-
}
98-
}
99-
10058
/**
10159
* @param PhpFileInfo $file
10260
*
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace SensioLabs\DeprecationDetector\Violation\Renderer\Console;
4+
5+
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper;
6+
use SensioLabs\DeprecationDetector\Violation\Violation;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class SimpleRenderer extends BaseRenderer
10+
{
11+
/**
12+
* @param OutputInterface $output
13+
* @param MessageHelper $messageHelper
14+
*/
15+
public function __construct(OutputInterface $output, MessageHelper $messageHelper)
16+
{
17+
parent::__construct($output, $messageHelper);
18+
}
19+
20+
/**
21+
* @param Violation[] $violations
22+
*/
23+
protected function printViolations(array $violations)
24+
{
25+
if (0 === count($violations)) {
26+
return;
27+
}
28+
29+
$tmpFile = null;
30+
foreach ($violations as $i => $violation) {
31+
if ($tmpFile !== $violation->getFile()) {
32+
$tmpFile = $violation->getFile();
33+
if (0 !== $i) {
34+
$this->output->writeln('');
35+
}
36+
$this->output->writeln($tmpFile->getRelativePathname());
37+
}
38+
39+
$this->output->writeln(sprintf(
40+
'Nr. %d line %d: %s',
41+
++$i,
42+
$violation->getLine(),
43+
$this->messageHelper->getViolationMessage($violation)
44+
));
45+
}
46+
}
47+
}

tests/Configuration/ConfigurationTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ public function testClassIsInitializable()
1616
'',
1717
true,
1818
true,
19-
'html.log'
19+
'html.log',
20+
null
2021
);
2122

2223
$this->assertInstanceOf('SensioLabs\DeprecationDetector\Configuration\Configuration', $configuration);
2324
}
2425

25-
public function testRuleSet()
26+
public function testConfiguration()
2627
{
2728
$configuration = new Configuration(
2829
'path/to/rule_set',
@@ -32,7 +33,8 @@ public function testRuleSet()
3233
'',
3334
true,
3435
true,
35-
'html.log'
36+
'html.log',
37+
null
3638
);
3739

3840
$this->assertEquals('path/to/rule_set', $configuration->ruleSet());
@@ -43,5 +45,6 @@ public function testRuleSet()
4345
$this->assertTrue($configuration->failOnDeprecation());
4446
$this->assertTrue($configuration->isVerbose());
4547
$this->assertEquals('html.log', $configuration->logHtml());
48+
$this->assertNull(null, $configuration->isSimpleOutput());
4649
}
4750
}

0 commit comments

Comments
 (0)