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

Authorize analyse of one file or specific directory #195

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/get-started.md
Expand Up @@ -35,6 +35,24 @@ You can also use `phpinsights` via Docker:
docker run -it --rm -v $(pwd):/app nunomaduro/phpinsights
```

## Analyse a sub-directory or a specific file

You can ask `phpinsights` to analyse only a directory or even a specific file by providing path with `analyse` command:

```bash
# For a directory
./vendor/bin/phpinsights analyse path/to/analyse
Jibbarth marked this conversation as resolved.
Show resolved Hide resolved

# For a file
./vendor/bin/phpinsights analyse path/to/analyse.php
```

In laravel, launch command as usual with your path:

```bash
php artisan insights path/to/analyse
```

## Allowed memory size of X bytes exhausted

If you encounter the error `Allowed memory size of XXXXX bytes exhausted`, the current workaround is to increase the memory limit:
Expand Down
24 changes: 22 additions & 2 deletions src/Application/Console/Commands/AnalyseCommand.php
Expand Up @@ -58,13 +58,19 @@ public function __invoke(InputInterface $input, OutputInterface $output): int

$directory = $this->getDirectory($input);

$isRootAnalyse = true;
foreach (Kernel::getRequiredFiles() as $file) {
if (! file_exists($directory . DIRECTORY_SEPARATOR . $file)) {
throw new \RuntimeException("The file `$file` must exist. You should run PHP Insights from the root of your project.");
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
$isRootAnalyse = false;
break;
}
}
$config = $this->getConfig($input, $directory);

$results = $this->analyser->analyse($style, $this->getConfig($input, $directory), $directory);
if (! $isRootAnalyse) {
$config = $this->excludeGlobalInsights($config);
}
$results = $this->analyser->analyse($style, $config, $directory);

$hasError = false;
if ($input->getOption('min-quality') > $results->getCodeQuality()) {
Expand Down Expand Up @@ -135,4 +141,18 @@ private function getDirectory(InputInterface $input): string

return $directory;
}

/**
* @param array<string, array> $config
*
* @return array<string, array>
*/
private function excludeGlobalInsights(array $config): array
{
foreach (Kernel::getGlobalInsights() as $insight) {
$config['remove'][] = $insight;
}

return $config;
}
}
3 changes: 1 addition & 2 deletions src/Domain/Insights/InsightCollectionFactory.php
Expand Up @@ -9,7 +9,6 @@
use NunoMaduro\PhpInsights\Domain\Contracts\Insight;
use NunoMaduro\PhpInsights\Domain\Contracts\Repositories\FilesRepository;
use NunoMaduro\PhpInsights\Domain\Exceptions\DirectoryNotFound;
use Symfony\Component\Finder\SplFileInfo;

/**
* @internal
Expand Down Expand Up @@ -48,7 +47,7 @@ public function __construct(FilesRepository $filesRepository, Analyser $analyser
public function get(array $metrics, array $config, string $dir): InsightCollection
{
try {
$files = array_map(static function (SplFileInfo $file) {
$files = array_map(static function (\SplFileInfo $file) {
return $file->getRealPath();
}, iterator_to_array($this->filesRepository->within($dir, $config['exclude'] ?? [])->getFiles()));
} catch (\InvalidArgumentException $exception) {
Expand Down
22 changes: 22 additions & 0 deletions src/Domain/Kernel.php
Expand Up @@ -4,6 +4,12 @@

namespace NunoMaduro\PhpInsights\Domain;

use NunoMaduro\PhpInsights\Domain\Insights\Composer\ComposerLockMustBeFresh;
use NunoMaduro\PhpInsights\Domain\Insights\Composer\ComposerMustBeValid;
use NunoMaduro\PhpInsights\Domain\Insights\Composer\ComposerMustContainName;
use NunoMaduro\PhpInsights\Domain\Insights\Composer\ComposerMustExist;
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenSecurityIssues;

/**
* @internal
*/
Expand Down Expand Up @@ -49,4 +55,20 @@ public static function getRequiredFiles(): array
// '.gitignore',
];
}

/**
* Returns the list of Insights required on root.
*
* @return array<string>
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function getGlobalInsights(): array
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
{
return [
ComposerMustBeValid::class,
ComposerLockMustBeFresh::class,
ComposerMustContainName::class,
ComposerMustExist::class,
ForbiddenSecurityIssues::class,
];
}
}
5 changes: 5 additions & 0 deletions src/Infrastructure/Repositories/LocalFilesRepository.php
Expand Up @@ -51,6 +51,11 @@ public function getFiles(): iterable
*/
public function within(string $directory, array $exclude = []): FilesRepository
{
if (! is_dir($directory) && is_file($directory)) {
$this->finder->append([$directory]);

return $this;
}
$this->finder->in([$directory])->notPath($exclude);

foreach ($exclude as $value) {
Expand Down
7 changes: 7 additions & 0 deletions tests/Infrastructure/Repositories/Fixtures/FileToInspect.php
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

final class FileToInspect
{
}
13 changes: 13 additions & 0 deletions tests/Infrastructure/Repositories/LocalFilesRepositoryTest.php
Expand Up @@ -24,4 +24,17 @@ public function testCanIgnoreBladeFiles() : void

self::assertEmpty($files);
}

public function testPassFileInsteadOfDirectory(): void
{
$finder = new Finder();

$repository = new LocalFilesRepository($finder);
$repository->within(__DIR__ . '/Fixtures/FileToInspect.php');
$files = iterator_to_array($repository->getFiles());

self::assertCount(1, $files);
self::assertInstanceOf(\SplFileInfo::class, $files[0]);
self::assertStringContainsString('/Fixtures/FileToInspect.php', $files[0]->getRealPath());
}
}