-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add regex search command for easy quick stats
- Loading branch information
1 parent
4002e0e
commit bf913bf
Showing
17 changed files
with
133 additions
and
55 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
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
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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\SwissKnife\Command; | ||
|
||
use Nette\Utils\Strings; | ||
use Rector\SwissKnife\Finder\PhpFilesFinder; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class SearchRegexCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly SymfonyStyle $symfonyStyle, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('search-regex'); | ||
|
||
$this->addArgument( | ||
'regex', | ||
InputArgument::REQUIRED, | ||
'Code snippet to look in PHP files in the whole codebase' | ||
); | ||
|
||
$this->addOption('project-directory', null, InputOption::VALUE_REQUIRED, 'Project directory', getcwd()); | ||
|
||
$this->setDescription('Search for regex in PHP files of the whole codebase'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$regex = (string) $input->getArgument('regex'); | ||
|
||
$projectDirectory = (string) $input->getOption('project-directory'); | ||
Assert::directory($projectDirectory); | ||
|
||
$phpFileInfos = PhpFilesFinder::find([$projectDirectory]); | ||
|
||
$message = sprintf('Going through %d *.php files', count($phpFileInfos)); | ||
$this->symfonyStyle->writeln($message); | ||
|
||
$this->symfonyStyle->writeln('Searching for regex: ' . $regex); | ||
$this->symfonyStyle->newLine(); | ||
|
||
$foundCasesCount = 0; | ||
$markedFileCount = 0; | ||
|
||
$progressBar = $this->symfonyStyle->createProgressBar(count($phpFileInfos)); | ||
|
||
foreach ($phpFileInfos as $phpFileInfo) { | ||
$matches = Strings::matchAll($phpFileInfo->getContents(), $regex); | ||
$currentMatchesCount = count($matches); | ||
if ($currentMatchesCount === 0) { | ||
continue; | ||
} | ||
|
||
$foundCasesCount += $currentMatchesCount; | ||
++$markedFileCount; | ||
|
||
$progressBar->advance(); | ||
} | ||
|
||
$progressBar->finish(); | ||
|
||
$this->symfonyStyle->newLine(2); | ||
$this->symfonyStyle->success(sprintf('Found %d cases in %d files', $foundCasesCount, $markedFileCount)); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.