From 797d358eed6ad09c319e4c8016702554fa48c637 Mon Sep 17 00:00:00 2001 From: MarvinKlemp Date: Fri, 11 Mar 2016 14:44:53 +0100 Subject: [PATCH] Implemented Path Formatters --- src/Configuration/Configuration.php | 26 ++++++++++--- src/Console/Command/CheckCommand.php | 10 ++++- src/DetectorFactory.php | 8 +++- .../Renderer/ConsoleOutputRenderer.php | 16 ++++++-- .../PathFormatter/DefaultFormatter.php | 11 ++++++ .../PathFormatter/PathFormatterInterface.php | 8 ++++ .../PathFormatter/ShortPathFormatter.php | 28 ++++++++++++++ tests/Configuration/ConfigurationTest.php | 6 ++- .../PathFormatter/DefaultFormatterTest.php | 28 ++++++++++++++ .../PathFormatter/ShortPathFormatterTest.php | 38 +++++++++++++++++++ 10 files changed, 166 insertions(+), 13 deletions(-) create mode 100644 src/Violation/Renderer/PathFormatter/DefaultFormatter.php create mode 100644 src/Violation/Renderer/PathFormatter/PathFormatterInterface.php create mode 100644 src/Violation/Renderer/PathFormatter/ShortPathFormatter.php create mode 100644 tests/Violation/Renderer/PathFormatter/DefaultFormatterTest.php create mode 100644 tests/Violation/Renderer/PathFormatter/ShortPathFormatterTest.php diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index d64dfd3..d9a152e 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -44,15 +44,21 @@ class Configuration */ private $logHtml; + /** + * @var string|bool + */ + private $shortPath; + /** * @param string $ruleSet * @param string $containerPath - * @param bool $noCache + * @param bool $noCache * @param string $cacheDir * @param string $filterMethodCalls - * @param bool $fail - * @param bool $verbose + * @param bool $fail + * @param bool $verbose * @param string $logHtml + * @param string|bool $shortPath */ public function __construct( $ruleSet, @@ -62,8 +68,9 @@ public function __construct( $filterMethodCalls, $fail, $verbose, - $logHtml) - { + $logHtml, + $shortPath + ) { $this->ruleSet = $ruleSet; $this->containerPath = $containerPath; $this->useCachedRuleSet = $noCache; @@ -72,6 +79,7 @@ public function __construct( $this->failOnDeprecation = $fail; $this->verbose = $verbose; $this->logHtml = $logHtml; + $this->shortPath = $shortPath; } public function overrideConfiguration() @@ -138,4 +146,12 @@ public function logHtml() { return $this->logHtml; } + + /** + * @return bool|string + */ + public function shortPath() + { + return $this->shortPath; + } } diff --git a/src/Console/Command/CheckCommand.php b/src/Console/Command/CheckCommand.php index d357bcd..ec9b283 100644 --- a/src/Console/Command/CheckCommand.php +++ b/src/Console/Command/CheckCommand.php @@ -35,6 +35,13 @@ protected function configure() 'The path to symfony container cache', 'app/cache/dev/appDevDebugProjectContainer.xml' ), + new InputOption( + 'short-paths', + null, + InputOption::VALUE_REQUIRED, + 'The ommited prefix path', + false + ), 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'), @@ -113,7 +120,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('short-paths') ); $factory = new DetectorFactory(); diff --git a/src/DetectorFactory.php b/src/DetectorFactory.php index 0df970b..d97bd24 100644 --- a/src/DetectorFactory.php +++ b/src/DetectorFactory.php @@ -45,6 +45,8 @@ 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\PathFormatter\DefaultFormatter; +use SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter\ShortPathFormatter; use SensioLabs\DeprecationDetector\Violation\ViolationChecker\ClassViolationChecker; use SensioLabs\DeprecationDetector\Violation\ViolationChecker\ComposedViolationChecker; use SensioLabs\DeprecationDetector\Violation\ViolationChecker\FunctionViolationChecker; @@ -305,7 +307,11 @@ private function getRenderer(Configuration $configuration, OutputInterface $outp return $factory->createHtmlOutputRenderer($logFilePath); } - return new ConsoleOutputRenderer($output, $messageHelper); + $formatter = ($prefix = $configuration->shortPath()) ? + new ShortPathFormatter($prefix) : + new DefaultFormatter(); + + return new ConsoleOutputRenderer($output, $messageHelper, $formatter); } /** diff --git a/src/Violation/Renderer/ConsoleOutputRenderer.php b/src/Violation/Renderer/ConsoleOutputRenderer.php index e925ba0..4466b3d 100644 --- a/src/Violation/Renderer/ConsoleOutputRenderer.php +++ b/src/Violation/Renderer/ConsoleOutputRenderer.php @@ -5,6 +5,7 @@ use PhpParser\Error; use SensioLabs\DeprecationDetector\FileInfo\PhpFileInfo; use SensioLabs\DeprecationDetector\Violation\Renderer\MessageHelper\MessageHelper; +use SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter\PathFormatterInterface; use SensioLabs\DeprecationDetector\Violation\Violation; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableCell; @@ -24,13 +25,20 @@ class ConsoleOutputRenderer implements RendererInterface private $messageHelper; /** - * @param OutputInterface $output - * @param MessageHelper $messageHelper + * @var PathFormatterInterface */ - public function __construct(OutputInterface $output, MessageHelper $messageHelper) + private $pathFormatter; + + /** + * @param OutputInterface $output + * @param MessageHelper $messageHelper + * @param PathFormatterInterface $formatter + */ + public function __construct(OutputInterface $output, MessageHelper $messageHelper, PathFormatterInterface $formatter) { $this->output = $output; $this->messageHelper = $messageHelper; + $this->pathFormatter = $formatter; } /** @@ -105,7 +113,7 @@ private function printErrors(array $errors) protected function getFileHeader(PhpFileInfo $file) { $cell = new TableCell( - sprintf('%s', $file->getPathname()), + sprintf('%s', $this->pathFormatter->format($file->getPathname())), array('colspan' => 3) ); diff --git a/src/Violation/Renderer/PathFormatter/DefaultFormatter.php b/src/Violation/Renderer/PathFormatter/DefaultFormatter.php new file mode 100644 index 0000000..33210a1 --- /dev/null +++ b/src/Violation/Renderer/PathFormatter/DefaultFormatter.php @@ -0,0 +1,11 @@ +omittedPrefix = $omittedPrefix; + } + + public function format($path) + { + if (substr($path, 0, strlen($this->omittedPrefix)) == $this->omittedPrefix) { + return substr($path, strlen($this->omittedPrefix)); + } + + return $path; + } +} diff --git a/tests/Configuration/ConfigurationTest.php b/tests/Configuration/ConfigurationTest.php index d79e760..43bedac 100644 --- a/tests/Configuration/ConfigurationTest.php +++ b/tests/Configuration/ConfigurationTest.php @@ -16,7 +16,8 @@ public function testClassIsInitializable() '', true, true, - 'html.log' + 'html.log', + false ); $this->assertInstanceOf('SensioLabs\DeprecationDetector\Configuration\Configuration', $configuration); @@ -32,7 +33,8 @@ public function testRuleSet() '', true, true, - 'html.log' + 'html.log', + false ); $this->assertEquals('path/to/rule_set', $configuration->ruleSet()); diff --git a/tests/Violation/Renderer/PathFormatter/DefaultFormatterTest.php b/tests/Violation/Renderer/PathFormatter/DefaultFormatterTest.php new file mode 100644 index 0000000..e69060d --- /dev/null +++ b/tests/Violation/Renderer/PathFormatter/DefaultFormatterTest.php @@ -0,0 +1,28 @@ +assertInstanceOf( + 'SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter\DefaultFormatter', + $formatter + ); + } + + public function testFormatReturnsOriginalString() + { + $formatter = new DefaultFormatter(); + + $this->assertEquals( + 'path/to/file', + $formatter->format('path/to/file') + ); + } +} diff --git a/tests/Violation/Renderer/PathFormatter/ShortPathFormatterTest.php b/tests/Violation/Renderer/PathFormatter/ShortPathFormatterTest.php new file mode 100644 index 0000000..f0debd2 --- /dev/null +++ b/tests/Violation/Renderer/PathFormatter/ShortPathFormatterTest.php @@ -0,0 +1,38 @@ +assertInstanceOf( + 'SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter\ShortPathFormatter', + $formatter + ); + } + + public function testFormatterReturnsStringIfIndexInNotInPath() + { + $formatter = new ShortPathFormatter('another/path/prefix'); + + $this->assertEquals( + 'path/to/file', + $formatter->format('path/to/file') + ); + } + + public function testFormatterRemovesIndexFromPath() + { + $formatter = new ShortPathFormatter('path/to/'); + + $this->assertEquals( + 'file', + $formatter->format('path/to/file') + ); + } +}