Skip to content

Commit

Permalink
Merge pull request #27 from laradumps/add-exactly-option
Browse files Browse the repository at this point in the history
Add `--exactly` option
  • Loading branch information
luanfreitasdev authored Jun 20, 2024
2 parents 8bf2eb8 + 513d7b4 commit 69fe22d
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ protected function configure(): void
->addOption('ignore', null, InputArgument::OPTIONAL, 'Directories to be ignored separated by comma')
->addOption('text', null, InputArgument::OPTIONAL, 'Texts that will be searched separated by a comma')
->addOption('ignore-files', null, InputArgument::OPTIONAL, 'Files that will be ignored separated by a comma')
->addArgument('stop-on-failure', InputArgument::OPTIONAL, 'Stop the search if a match is found');
->addArgument('stop-on-failure', InputArgument::OPTIONAL, 'Stop the search if a match is found')
->addOption('exactly', null, InputArgument::OPTIONAL, 'Search for exact occurrences');
}

/**
Expand Down Expand Up @@ -116,6 +117,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

if ($input->getOption('exactly')) {
foreach ($this->prepareCustomTextToSearch($input) as $search) {
if (strpos($lineContent, ltrim($search))) {
$contains = true;

break;
}
}
}

if ($contains && !$ignore) {
$matches[] = $this->addMatchToDisplay($file, $lineContent, $line);

Expand Down Expand Up @@ -204,7 +215,15 @@ private function prepareTextToSearch(InputInterface $input): array

$values = explode(',', $checkInFor);

$mergedValues = array_unique(array_merge(explode(',', $this->defaultTextToSearch), $values));
$mergedValues = array_unique(
array_merge(
explode(
',',
$this->defaultTextToSearch
),
$values
)
);

foreach ($mergedValues as $search) {
$search = trim($search);
Expand All @@ -223,6 +242,29 @@ private function prepareTextToSearch(InputInterface $input): array
return $textToSearch;
}

private function prepareCustomTextToSearch(InputInterface $input): array
{
$textToSearch = [];

$checkInFor = explode(',', $input->getOption('text') ?? '');

foreach ($checkInFor as $search) {
$search = trim($search);

if (strlen($search) > 0) {
$textToSearch[] = ' ' . $search;
$textToSearch[] = $search;
$textToSearch[] = '//' . $search;
$textToSearch[] = '->' . $search;
$textToSearch[] = $search . '(';
$textToSearch[] = '@' . $search;
$textToSearch[] = ' @' . $search;
}
}

return $textToSearch;
}

private function prepareTextToIgnore(InputInterface $input): array
{
$array = [];
Expand Down

0 comments on commit 69fe22d

Please sign in to comment.