Skip to content

Commit

Permalink
Merge branch 'feature/php-parser-3x' of mkgithub.com:MarvinKlemp/depr…
Browse files Browse the repository at this point in the history
…ecation-detector into feature/php-parser-3x
  • Loading branch information
MarvinKlemp committed Apr 8, 2017
2 parents 2a60b08 + 73d36da commit be22bb5
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 62 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ You can get a list of all options and arguments by running
$ deprecation-detector check --help
```

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`.

```bash
$ deprecation-detector check src/ vendor/ --output=simple
```

## Excluding deprecated method calls

You can exclude deprecated method calls by using the `filter-methods` option. This option takes a comma separated list of method references to exclude.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"php": ">=5.5.9 || ^7.0",
"nikic/php-parser": "3.*",
"nikic/php-parser": "~3.0",
"symfony/console": "^2.6 || ^3.0",
"symfony/finder": "^2.6 || ^3.0",
"phpdocumentor/reflection-docblock": "~2.0",
Expand Down
18 changes: 17 additions & 1 deletion src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class Configuration
*/
private $logHtml;

/**
* @var string
*/
private $output;

/**
* @param string $ruleSet
* @param string $containerPath
Expand All @@ -53,6 +58,7 @@ class Configuration
* @param bool $fail
* @param bool $verbose
* @param string $logHtml
* @param string $output
*/
public function __construct(
$ruleSet,
Expand All @@ -62,7 +68,8 @@ public function __construct(
$filterMethodCalls,
$fail,
$verbose,
$logHtml)
$logHtml,
$output)
{
$this->ruleSet = $ruleSet;
$this->containerPath = $containerPath;
Expand All @@ -72,6 +79,7 @@ public function __construct(
$this->failOnDeprecation = $fail;
$this->verbose = $verbose;
$this->logHtml = $logHtml;
$this->output = $output;
}

public function overrideConfiguration()
Expand Down Expand Up @@ -138,4 +146,12 @@ public function logHtml()
{
return $this->logHtml;
}

/**
* @return bool
*/
public function isSimpleOutput()
{
return $this->output === 'simple';
}
}
4 changes: 3 additions & 1 deletion src/Console/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function configure()
new InputOption('no-cache', null, InputOption::VALUE_NONE, 'Disable rule set cache'),
new InputOption('cache-dir', null, InputOption::VALUE_REQUIRED, 'Cache directory', '.rules/'),
new InputOption('log-html', null, InputOption::VALUE_REQUIRED, 'Generate HTML report'),
new InputOption('output', null, InputOption::VALUE_REQUIRED, 'Change the output mode'),
new InputOption('filter-methods', null, InputOption::VALUE_OPTIONAL, 'Filter methods', ''),
new InputOption('fail', null, InputOption::VALUE_NONE, 'Fails, if any deprecation is detected'),
)
Expand Down Expand Up @@ -113,7 +114,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$input->getOption('filter-methods'),
$input->getOption('fail'),
$input->getOption('verbose'),
$input->getOption('log-html')
$input->getOption('log-html'),
$input->getOption('output')
);

$factory = new DetectorFactory();
Expand Down
11 changes: 8 additions & 3 deletions src/DetectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
use SensioLabs\DeprecationDetector\TypeGuessing\SymbolTable\SymbolTable;
use SensioLabs\DeprecationDetector\TypeGuessing\SymbolTable\Visitor\SymbolTableVariableResolverVisitor;
use SensioLabs\DeprecationDetector\TypeGuessing\Symfony\ContainerReader;
use SensioLabs\DeprecationDetector\Violation\Renderer\ConsoleOutputRenderer;
use SensioLabs\DeprecationDetector\Violation\Renderer\Console\DefaultRenderer;
use SensioLabs\DeprecationDetector\Violation\Renderer\Html\RendererFactory;
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\Message\ClassViolationMessage;
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\Message\FunctionViolationMessage;
Expand All @@ -45,6 +45,7 @@
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\Message\MethodViolationMessage;
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\Message\SuperTypeViolationMessage;
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper;
use SensioLabs\DeprecationDetector\Violation\Renderer\Console\SimpleRenderer;
use SensioLabs\DeprecationDetector\Violation\ViolationChecker\ClassViolationChecker;
use SensioLabs\DeprecationDetector\Violation\ViolationChecker\ComposedViolationChecker;
use SensioLabs\DeprecationDetector\Violation\ViolationChecker\FunctionViolationChecker;
Expand Down Expand Up @@ -293,7 +294,7 @@ private function getViolationFilter(Configuration $configuration)
* @param Configuration $configuration
* @param OutputInterface $output
*
* @return ConsoleOutputRenderer|Violation\Renderer\Html\Renderer
* @return DefaultRenderer|Violation\Renderer\Html\Renderer
*/
private function getRenderer(Configuration $configuration, OutputInterface $output)
{
Expand All @@ -305,7 +306,11 @@ private function getRenderer(Configuration $configuration, OutputInterface $outp
return $factory->createHtmlOutputRenderer($logFilePath);
}

return new ConsoleOutputRenderer($output, $messageHelper);
if ($configuration->isSimpleOutput()) {
return new SimpleRenderer($output, $messageHelper);
}

return new DefaultRenderer($output, $messageHelper);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/FileInfo/PhpFileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function hasClassDeprecation($className)
*/
public function getClassDeprecation($className)
{
return ($this->hasClassDeprecation($className) ? $this->classDeprecations[$className] : null);
return $this->hasClassDeprecation($className) ? $this->classDeprecations[$className] : null;
}

/**
Expand Down Expand Up @@ -324,7 +324,7 @@ public function hasInterfaceDeprecation($interfaceName)
*/
public function getInterfaceDeprecation($interfaceName)
{
return ($this->hasInterfaceDeprecation($interfaceName) ? $this->interfaceDeprecations[$interfaceName] : null);
return $this->hasInterfaceDeprecation($interfaceName) ? $this->interfaceDeprecations[$interfaceName] : null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/RuleSet/Loader/Composer/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function fromArray(array $package)
*/
public function generatePackageKey()
{
return str_replace('/', '_', $this->name).'_'.$this->version;
return str_replace(array('/', '\\', ':'), '_', $this->name).'_'.$this->version;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/RuleSet/Loader/DirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public function loadRuleSet($path)
*/
private function generateDirectoryKey($path)
{
return str_replace('/', '_', $path);
return str_replace(array('/', '\\', ':'), '_', $path);
}
}
2 changes: 1 addition & 1 deletion src/TypeGuessing/SymbolTable/Resolver/SymfonyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ protected function getType($context, $methodName, Node $node = null)
*/
protected function isController($type)
{
return (substr($type, -10) === 'Controller');
return substr($type, -10) === 'Controller';
}
}
68 changes: 68 additions & 0 deletions src/Violation/Renderer/Console/BaseRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace SensioLabs\DeprecationDetector\Violation\Renderer\Console;

use PhpParser\Error;
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper;
use SensioLabs\DeprecationDetector\Violation\Renderer\RendererInterface;
use SensioLabs\DeprecationDetector\Violation\Violation;
use Symfony\Component\Console\Output\OutputInterface;

abstract class BaseRenderer implements RendererInterface
{
/**
* @var OutputInterface
*/
protected $output;

/**
* @var MessageHelper
*/
protected $messageHelper;

/**
* @param OutputInterface $output
* @param MessageHelper $messageHelper
*/
public function __construct(OutputInterface $output, MessageHelper $messageHelper)
{
$this->output = $output;
$this->messageHelper = $messageHelper;
}

/**
* @param Violation[] $violations
* @param Error[] $errors
*/
public function renderViolations(array $violations, array $errors)
{
$this->printViolations($violations);
$this->printErrors($errors);
}

/**
* @param Violation[] $violations
*/
abstract protected function printViolations(array $violations);

/**
* @param Error[] $errors
*/
protected function printErrors(array $errors)
{
if (0 === count($errors)) {
return;
}

$this->output->writeln('');
$this->output->writeln('<error>Your project contains invalid code:</error>');
foreach ($errors as $error) {
$this->output->writeln(
sprintf(
'<error>%s</error>',
$error->getRawMessage()
)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

namespace SensioLabs\DeprecationDetector\Violation\Renderer;
namespace SensioLabs\DeprecationDetector\Violation\Renderer\Console;

use PhpParser\Error;
use SensioLabs\DeprecationDetector\FileInfo\PhpFileInfo;
use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper;
use SensioLabs\DeprecationDetector\Violation\Violation;
Expand All @@ -11,42 +10,21 @@
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Output\OutputInterface;

class ConsoleOutputRenderer implements RendererInterface
class DefaultRenderer extends BaseRenderer
{
/**
* @var OutputInterface
*/
protected $output;

/**
* @var MessageHelper
*/
private $messageHelper;

/**
* @param OutputInterface $output
* @param MessageHelper $messageHelper
*/
public function __construct(OutputInterface $output, MessageHelper $messageHelper)
{
$this->output = $output;
$this->messageHelper = $messageHelper;
parent::__construct($output, $messageHelper);
}

/**
* @param Violation[] $violations
* @param Error[] $errors
*/
public function renderViolations(array $violations, array $errors)
{
$this->printViolations($violations);
$this->printErrors($errors);
}

/**
* @param Violation[] $violations
*/
private function printViolations(array $violations)
protected function printViolations(array $violations)
{
if (0 === count($violations)) {
return;
Expand Down Expand Up @@ -77,26 +55,6 @@ private function printViolations(array $violations)
$table->render();
}

/**
* @param Error[] $errors
*/
private function printErrors(array $errors)
{
if (0 === count($errors)) {
return;
}

$this->output->writeln('<error>Your project contains invalid code:</error>');
foreach ($errors as $error) {
$this->output->writeln(
sprintf(
'<error>%s</error>',
$error->getRawMessage()
)
);
}
}

/**
* @param PhpFileInfo $file
*
Expand Down
47 changes: 47 additions & 0 deletions src/Violation/Renderer/Console/SimpleRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace SensioLabs\DeprecationDetector\Violation\Renderer\Console;

use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper;
use SensioLabs\DeprecationDetector\Violation\Violation;
use Symfony\Component\Console\Output\OutputInterface;

class SimpleRenderer extends BaseRenderer
{
/**
* @param OutputInterface $output
* @param MessageHelper $messageHelper
*/
public function __construct(OutputInterface $output, MessageHelper $messageHelper)
{
parent::__construct($output, $messageHelper);
}

/**
* @param Violation[] $violations
*/
protected function printViolations(array $violations)
{
if (0 === count($violations)) {
return;
}

$tmpFile = null;
foreach ($violations as $i => $violation) {
if ($tmpFile !== $violation->getFile()) {
$tmpFile = $violation->getFile();
if (0 !== $i) {
$this->output->writeln('');
}
$this->output->writeln($tmpFile->getRelativePathname());
}

$this->output->writeln(sprintf(
'Nr. %d line %d: %s',
++$i,
$violation->getLine(),
$this->messageHelper->getViolationMessage($violation)
));
}
}
}
4 changes: 2 additions & 2 deletions src/Visitor/Usage/FindLanguageDeprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function setPhpFileInfo(PhpFileInfo $phpFileInfo)
*/
public function enterNode(Node $node)
{
if ($node instanceof Node\Expr\AssignRef) {
if ($node instanceof Node\Expr\AssignRef && $node->expr instanceof Node\Expr\New_) {
$this->phpFileInfo->addDeprecatedLanguageUsage(
new DeprecatedLanguageUsage('assign by reference(&=)', 'Since PHP 5.3 use normal assignment instead.', $node->getLine())
new DeprecatedLanguageUsage('Assigning the return value of new by reference is now deprecated.', 'Since PHP 5.3 use normal assignment instead.', $node->getLine())
);
}

Expand Down
Loading

0 comments on commit be22bb5

Please sign in to comment.