Skip to content

Commit e5bd7dc

Browse files
committed
misc
1 parent 25bed4a commit e5bd7dc

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

src/Command/DuplicatedDefinitionsCommand.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,15 @@ protected function configure(): void
4141
'Find duplicated definitions in *Context.php, use just one to keep definitions clear and to the point'
4242
);
4343

44-
$this->addArgument(
45-
'test-directory',
46-
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
47-
'One or more paths to check or *.Context.php and feature.yml files'
48-
);
44+
$this->addArgument('test-directory', InputArgument::REQUIRED, 'Director with *.Context.php definition files');
4945
}
5046

5147
protected function execute(InputInterface $input, OutputInterface $output): int
5248
{
53-
$testDirectories = (array) $input->getArgument('test-directory');
54-
Assert::allDirectory($testDirectories);
49+
$testDirectory = (string) $input->getArgument('test-directory');
50+
Assert::directory($testDirectory);
5551

56-
$contextFileInfos = $this->behatMetafilesFinder->findContextFiles($testDirectories);
52+
$contextFileInfos = $this->behatMetafilesFinder->findContextFiles([$testDirectory]);
5753

5854
if ($contextFileInfos === []) {
5955
$this->symfonyStyle->error('No *.Context files found. Please provide correct test directory');
@@ -100,31 +96,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
10096
}
10197

10298
// keep only duplicated
103-
foreach ($classMethodContextDefinitionByClassMethodHash as $hash => $classAndMethods) {
104-
if (count($classAndMethods) < 2) {
105-
unset($classMethodContextDefinitionByClassMethodHash[$hash]);
106-
}
107-
108-
}
99+
$classMethodContextDefinitionByClassMethodHash = $this->filterOutNotDuplicated($classMethodContextDefinitionByClassMethodHash);
109100

110-
foreach ($classMethodContextDefinitionByClassMethodHash as $classAndMethods) {
111-
$this->symfonyStyle->warning('Found duplicated class classMethod contents');
101+
foreach ($classMethodContextDefinitionByClassMethodHash as $i => $classAndMethods) {
102+
$this->symfonyStyle->section(sprintf('%d)', $i + 1));
112103

113104
foreach ($classAndMethods as $classMethodContextDefinition) {
114105
/** @var ClassMethodContextDefinition $classMethodContextDefinition */
106+
$relativeFilePath = substr($classMethodContextDefinition->getFilePath(), strlen($testDirectory ) + 1);
107+
115108
$this->symfonyStyle->writeln(
116-
' * ' . $classMethodContextDefinition->getClass() . '::' . $classMethodContextDefinition->getMethodName() . ' in '
117-
);
118-
$this->symfonyStyle->writeln(
119-
$classMethodContextDefinition->getFilePath() . ':' . $classMethodContextDefinition->getMethodLine()
109+
$relativeFilePath . ':' . $classMethodContextDefinition->getMethodLine()
120110
);
121111

122-
$this->symfonyStyle->writeln('Mask: <fg=green>' . $classMethodContextDefinition->getMask() . '</>');
112+
$this->symfonyStyle->writeln('Mask: <fg=green>"' . $classMethodContextDefinition->getMask() . '"</>');
123113
$this->symfonyStyle->newLine();
124114
}
115+
116+
$this->symfonyStyle->newLine();
125117
}
126118

127-
// $this->symfonyStyle->error(sprintf('Found %d duplicated class classMethod contents', count($classMethodContextDefinitionByClassMethodHash)));
119+
$this->symfonyStyle->error(sprintf('Found %d duplicated class classMethod contents', count($classMethodContextDefinitionByClassMethodHash)));
128120

129121
return Command::FAILURE;
130122
}
@@ -134,4 +126,21 @@ private function createClassMethodHash(ClassMethod $classMethod): string
134126
$printedClassMethod = $this->printerStandard->prettyPrint((array) $classMethod->stmts);
135127
return sha1($printedClassMethod);
136128
}
129+
130+
/**
131+
* @template TItem as object
132+
*
133+
* @param TItem[] $items
134+
* @return array<int, TItem>
135+
*/
136+
private function filterOutNotDuplicated(array $items): array
137+
{
138+
foreach ($items as $hash => $classAndMethods) {
139+
if (count($classAndMethods) < 2) {
140+
unset($items[$hash]);
141+
}
142+
}
143+
144+
return array_values($items);
145+
}
137146
}

0 commit comments

Comments
 (0)