-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PSR-4] Add "find-multi-classes" command to check files with multiple…
… classes (#15)
- Loading branch information
1 parent
cf9e445
commit e254733
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCI\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symplify\EasyCI\Finder\MultipleClassInOneFileFinder; | ||
|
||
final class FindMultiClassesCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly MultipleClassInOneFileFinder $multipleClassInOneFileFinder, | ||
private readonly SymfonyStyle $symfonyStyle, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('find-multi-classes'); | ||
|
||
$this->setDescription('Find multiple classes in one file'); | ||
|
||
$this->addArgument( | ||
'sources', | ||
InputArgument::REQUIRED | InputArgument::IS_ARRAY, | ||
'Path to source to analyse' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
/** @var string[] $source */ | ||
$source = $input->getArgument('sources'); | ||
|
||
$multipleClassesByFile = $this->multipleClassInOneFileFinder->findInDirectories($source); | ||
if ($multipleClassesByFile === []) { | ||
$this->symfonyStyle->success('No files with 2+ classes found'); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
foreach ($multipleClassesByFile as $file => $classes) { | ||
$message = sprintf('File "%s" has %d classes', $file, count($classes)); | ||
$this->symfonyStyle->section($message); | ||
$this->symfonyStyle->listing($classes); | ||
} | ||
|
||
return self::FAILURE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCI\Finder; | ||
|
||
use Symplify\EasyCI\RobotLoader\PhpClassLoader; | ||
|
||
final readonly class MultipleClassInOneFileFinder | ||
{ | ||
public function __construct( | ||
private PhpClassLoader $phpClassLoader | ||
) { | ||
} | ||
|
||
/** | ||
* @param string[] $directories | ||
* @return string[][] | ||
*/ | ||
public function findInDirectories(array $directories): array | ||
{ | ||
$fileByClasses = $this->phpClassLoader->load($directories); | ||
|
||
$classesByFile = []; | ||
foreach ($fileByClasses as $class => $file) { | ||
$classesByFile[$file][] = $class; | ||
} | ||
|
||
return array_filter($classesByFile, static fn(array $classes): bool => count($classes) >= 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Symplify\EasyCI\RobotLoader; | ||
|
||
use Nette\Loaders\RobotLoader; | ||
|
||
final class PhpClassLoader | ||
{ | ||
/** | ||
* @param string[] $directories | ||
* @return array<string, string> | ||
*/ | ||
public function load(array $directories): array | ||
{ | ||
$robotLoader = new RobotLoader(); | ||
$robotLoader->addDirectory(...$directories); | ||
$robotLoader->setTempDirectory(sys_get_temp_dir() . '/multiple-classes'); | ||
$robotLoader->rebuild(); | ||
|
||
return $robotLoader->getIndexedClasses(); | ||
} | ||
} |