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

move resolving files to be analysed to its own class #155

Merged
merged 1 commit into from
Apr 20, 2018
Merged
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
3 changes: 3 additions & 0 deletions services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

<service id="SensioLabs\Deptrac\Updater\DeptracStrategy"/>

<service id="SensioLabs\Deptrac\FileResolver"/>

<service id="Humbug\SelfUpdate\Updater">
<argument>null</argument>
<!-- disable signed releases, otherwise will search for a .pubkey file for the PHAR -->
Expand All @@ -72,6 +74,7 @@

<service id="SensioLabs\Deptrac\Command\AnalyzeCommand" public="true">
<argument type="service" id="SensioLabs\Deptrac\Configuration\Loader"/>
<argument type="service" id="SensioLabs\Deptrac\FileResolver"/>
<argument type="service" id="Symfony\Component\EventDispatcher\EventDispatcher"/>
<argument type="service" id="SensioLabs\AstRunner\AstRunner"/>
<argument type="service" id="SensioLabs\Deptrac\OutputFormatterFactory"/>
Expand Down
46 changes: 10 additions & 36 deletions src/Command/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use SensioLabs\Deptrac\ClassNameLayerResolver;
use SensioLabs\Deptrac\ClassNameLayerResolverCacheDecorator;
use SensioLabs\Deptrac\CollectorFactory;
use SensioLabs\Deptrac\Configuration\Configuration;
use SensioLabs\Deptrac\Configuration\Exception\MissingFileException;
use SensioLabs\Deptrac\Configuration\Loader as ConfigurationLoader;
use SensioLabs\Deptrac\DependencyContext;
Expand All @@ -16,6 +15,7 @@
use SensioLabs\Deptrac\DependencyEmitter\InheritanceDependencyEmitter;
use SensioLabs\Deptrac\DependencyInheritanceFlatter;
use SensioLabs\Deptrac\DependencyResult;
use SensioLabs\Deptrac\FileResolver;
use SensioLabs\Deptrac\Formatter\ConsoleFormatter;
use SensioLabs\Deptrac\OutputFormatterFactory;
use SensioLabs\Deptrac\RulesetEngine;
Expand All @@ -24,26 +24,28 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Finder\Finder;

class AnalyzeCommand extends Command
{
private $configurationLoader;
protected $dispatcher;
protected $astRunner;
protected $formatterFactory;
protected $rulesetEngine;
protected $collectorFactory;
private $fileResolver;
private $dispatcher;
private $astRunner;
private $formatterFactory;
private $rulesetEngine;
private $collectorFactory;

public function __construct(
ConfigurationLoader $configurationLoader,
FileResolver $fileResolver,
EventDispatcherInterface $dispatcher,
AstRunner $astRunner,
OutputFormatterFactory $formatterFactory,
RulesetEngine $rulesetEngine,
CollectorFactory $collectorFactory
) {
$this->configurationLoader = $configurationLoader;
$this->fileResolver = $fileResolver;
$this->dispatcher = $dispatcher;
$this->astRunner = $astRunner;
$this->formatterFactory = $formatterFactory;
Expand Down Expand Up @@ -81,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
new ConsoleFormatter($this->dispatcher, $output);

$parser = new NikicPhpParser();
$astMap = $this->astRunner->createAstMapByFiles($parser, $this->collectFiles($configuration));
$astMap = $this->astRunner->createAstMapByFiles($parser, $this->fileResolver->resolve($configuration));

$dependencyResult = new DependencyResult();

Expand Down Expand Up @@ -132,34 +134,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
return count($violations) ? 1 : 0;
}

/**
* @param Configuration $configuration
*
* @return \SplFileInfo[]
*/
private function collectFiles(Configuration $configuration): array
{
$files = iterator_to_array(
(new Finder())
->in($configuration->getPaths())
->name('*.php')
->files()
->followLinks()
->ignoreUnreadableDirs(true)
->ignoreVCS(true)
);

return array_filter($files, function (\SplFileInfo $fileInfo) use ($configuration) {
foreach ($configuration->getExcludeFiles() as $excludeFiles) {
if (preg_match('/'.$excludeFiles.'/i', $fileInfo->getPathname())) {
return false;
}
}

return true;
});
}

protected function printBanner(OutputInterface $output)
{
$output->writeln("\n<comment>deptrac is alpha, not production ready.\nplease help us and report feedback / bugs.</comment>\n");
Expand Down
41 changes: 41 additions & 0 deletions src/FileResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace SensioLabs\Deptrac;

use SensioLabs\Deptrac\Configuration\Configuration;
use Symfony\Component\Finder\Finder;

class FileResolver
{
/**
* @param Configuration $configuration
*
* @throws \InvalidArgumentException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will this exception ever be thrown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finder::in()-Method will throw this exception if a directory does not exists

*
* @return \SplFileInfo[]
*/
public function resolve(Configuration $configuration): array
{
$files = iterator_to_array(
(new Finder())
->in($configuration->getPaths())
->name('*.php')
->files()
->followLinks()
->ignoreUnreadableDirs(true)
->ignoreVCS(true)
);

return array_filter($files, function (\SplFileInfo $fileInfo) use ($configuration) {
foreach ($configuration->getExcludeFiles() as $excludeFiles) {
if (preg_match('/'.$excludeFiles.'/i', $fileInfo->getPathname())) {
return false;
}
}

return true;
});
}
}