Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Path Formatters #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -62,8 +68,9 @@ public function __construct(
$filterMethodCalls,
$fail,
$verbose,
$logHtml)
{
$logHtml,
$shortPath
) {
$this->ruleSet = $ruleSet;
$this->containerPath = $containerPath;
$this->useCachedRuleSet = $noCache;
Expand All @@ -72,6 +79,7 @@ public function __construct(
$this->failOnDeprecation = $fail;
$this->verbose = $verbose;
$this->logHtml = $logHtml;
$this->shortPath = $shortPath;
}

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

/**
* @return bool|string
*/
public function shortPath()
{
return $this->shortPath;
}
}
10 changes: 9 additions & 1 deletion src/Console/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion src/DetectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/Violation/Renderer/ConsoleOutputRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -105,7 +113,7 @@ private function printErrors(array $errors)
protected function getFileHeader(PhpFileInfo $file)
{
$cell = new TableCell(
sprintf('<comment>%s</comment>', $file->getPathname()),
sprintf('<comment>%s</comment>', $this->pathFormatter->format($file->getPathname())),
array('colspan' => 3)
);

Expand Down
11 changes: 11 additions & 0 deletions src/Violation/Renderer/PathFormatter/DefaultFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter;

class DefaultFormatter implements PathFormatterInterface
{
public function format($path)
{
return $path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter;

interface PathFormatterInterface
{
public function format($path);
}
28 changes: 28 additions & 0 deletions src/Violation/Renderer/PathFormatter/ShortPathFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter;

class ShortPathFormatter implements PathFormatterInterface
{
/**
* @var string
*/
private $omittedPrefix;

/**
* @param string $omittedPrefix
*/
public function __construct($omittedPrefix)
{
$this->omittedPrefix = $omittedPrefix;
}

public function format($path)
{
if (substr($path, 0, strlen($this->omittedPrefix)) == $this->omittedPrefix) {
return substr($path, strlen($this->omittedPrefix));
}

return $path;
}
}
6 changes: 4 additions & 2 deletions tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function testClassIsInitializable()
'',
true,
true,
'html.log'
'html.log',
false
);

$this->assertInstanceOf('SensioLabs\DeprecationDetector\Configuration\Configuration', $configuration);
Expand All @@ -32,7 +33,8 @@ public function testRuleSet()
'',
true,
true,
'html.log'
'html.log',
false
);

$this->assertEquals('path/to/rule_set', $configuration->ruleSet());
Expand Down
28 changes: 28 additions & 0 deletions tests/Violation/Renderer/PathFormatter/DefaultFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace SensioLabs\DeprecationDetector\Tests\Violation\Renderer\PathFormatter;

use SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter\DefaultFormatter;

class DefaultFormatterTest extends \PHPUnit_Framework_TestCase
{
public function testClassIsInitializable()
{
$formatter = new DefaultFormatter();

$this->assertInstanceOf(
'SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter\DefaultFormatter',
$formatter
);
}

public function testFormatReturnsOriginalString()
{
$formatter = new DefaultFormatter();

$this->assertEquals(
'path/to/file',
$formatter->format('path/to/file')
);
}
}
38 changes: 38 additions & 0 deletions tests/Violation/Renderer/PathFormatter/ShortPathFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace SensioLabs\DeprecationDetector\Tests\Violation\Renderer\PathFormatter;

use SensioLabs\DeprecationDetector\Violation\Renderer\PathFormatter\ShortPathFormatter;

class ShortPathFormatterTest extends \PHPUnit_Framework_TestCase
{
public function testClassIsInitializable()
{
$formatter = new ShortPathFormatter('');

$this->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')
);
}
}