Skip to content

Commit 8c90708

Browse files
committed
Get psalm passing
1 parent a02470b commit 8c90708

File tree

5 files changed

+56
-17
lines changed

5 files changed

+56
-17
lines changed

src/Command/ChangePathPrefix.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,24 @@ protected function configure(): void
8787

8888
protected function execute(InputInterface $input, OutputInterface $output): int
8989
{
90-
$fixer = new PathFixer(
91-
$input->getArgument('original_prefix'),
92-
$input->getArgument('replacement_prefix')
93-
);
90+
$directory = $input->getArgument('directory_to_search');
91+
$originalPrefix = $input->getArgument('original_prefix');
92+
$replacementPrefix = $input->getArgument('replacement_prefix');
93+
94+
/** @var bool $asClover */
95+
$asClover = $input->getOption('clover');
96+
97+
if (!is_string($directory) || !is_string($originalPrefix) || !is_string($replacementPrefix)) {
98+
throw new \InvalidArgumentException('Argument(s) not specified as string');
99+
}
100+
101+
$fixer = new PathFixer($originalPrefix, $replacementPrefix);
94102

95103
try {
96-
$files = $this->finder->findCoverage($input->getArgument('directory_to_search'));
104+
$files = $this->finder->findCoverage($directory);
97105
$output->writeln(sprintf('%d .cov files found', count($files)));
98106

107+
/** @var array<string, CodeCoverage> $files */
99108
$files = $this->iterateCoverageFiles(
100109
$files,
101110
$fixer
@@ -104,10 +113,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
104113
$this->writer->setFiles($files);
105114

106115
if ($path = $input->getOption('merge')) {
116+
if (!is_string($path)) {
117+
throw new \InvalidArgumentException('Path to merged file must be provided as a string');
118+
}
119+
107120
$this->writer->merge($path);
108121
}
109122

110-
$this->writer->write($input->getOption('clover'));
123+
$this->writer->write($asClover);
111124
} catch (\Exception $ex) {
112125
$output->writeln($ex->getMessage());
113126
return 1;
@@ -116,13 +129,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
116129
return 0;
117130
}
118131

132+
/**
133+
* @param string[] $files
134+
* @param PathFixer $fixer
135+
* @return CodeCoverage[]
136+
*/
119137
protected function iterateCoverageFiles(array $files, PathFixer $fixer): array
120138
{
121-
return array_map(function(array $file) use ($fixer) {
122-
$coverage = $this->loader->loadCoverage($file[0]);
139+
return array_map(function(string $file) use ($fixer) {
140+
$coverage = $this->loader->loadCoverage($file);
141+
142+
$data = $coverage->getData();
143+
$data = $fixer->fix($data);
123144

124-
$data = $fixer->fix($coverage->getData());
125-
$whiteList = $fixer->fix( $coverage->filter()->getWhitelistedFiles());
145+
$whiteList = $coverage->filter()->getWhitelistedFiles();
146+
$whiteList = $fixer->fix($whiteList);
126147

127148
$filter = new Filter();
128149
$filter->setWhitelistedFiles($whiteList);

src/Service/CoverageLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class CoverageLoader
1515
*/
1616
public function loadCoverage(string $file): CodeCoverage
1717
{
18+
/**
19+
* @psalm-suppress MixedAssignment
20+
* @psalm-suppress UnresolvableInclude
21+
*/
1822
$coverage = include $file;
1923

2024
if (!($coverage instanceof CodeCoverage)) {

src/Service/FileFinder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
class FileFinder
1313
{
14+
/**
15+
* @param string $directory
16+
* @return string[]
17+
*/
1418
public function findCoverage(string $directory): array
1519
{
1620
$path = realpath($directory);
@@ -19,6 +23,7 @@ public function findCoverage(string $directory): array
1923
$iterator = new RecursiveIteratorIterator($directory);
2024
$filtered = new RegexIterator($iterator, '/^.+\.cov$/i', RecursiveRegexIterator::GET_MATCH);
2125

22-
return iterator_to_array($filtered);
26+
/** @var string[] */
27+
return array_keys(iterator_to_array($filtered));
2328
}
2429
}

src/Service/FileWriter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
class FileWriter
1212
{
1313
/**
14-
* @var CodeCoverage[]
14+
* @var array<string, CodeCoverage>
1515
*/
1616
private $files;
1717

1818
/**
1919
* FileWriter constructor.
2020
*
21-
* @param CodeCoverage[] $files
21+
* @param array<string, CodeCoverage> $files
2222
*/
2323
public function __construct(array $files = [])
2424
{
@@ -40,7 +40,7 @@ public function addFile(string $path, CodeCoverage $coverage): FileWriter
4040
}
4141

4242
/**
43-
* @return CodeCoverage[]
43+
* @return array<string, CodeCoverage>
4444
*/
4545
public function getFiles(): array
4646
{
@@ -69,7 +69,7 @@ public function merge(string $path): FileWriter
6969
/**
7070
* Fluent method that allows the replacement of the list of files to write
7171
*
72-
* @param CodeCoverage[] $files
72+
* @param array<string, CodeCoverage> $files
7373
* @return FileWriter
7474
*/
7575
public function setFiles(array $files): FileWriter
@@ -88,6 +88,7 @@ public function setFiles(array $files): FileWriter
8888
*/
8989
public function write(bool $asClover = false): void
9090
{
91+
/** @psalm-suppress MixedPropertyTypeCoercion */
9192
array_walk($this->files, function(CodeCoverage $coverage, string $path) use ($asClover) {
9293
if ($asClover) {
9394
$filename = basename($path, '.cov');

src/Service/PathFixer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,17 @@ public function fix(array $data): array
3737
$originalPrefix = $this->originalPrefix;
3838
$replacementPrefix = $this->replacementPrefix;
3939

40-
return array_combine(array_map(function(string $el) use ($originalPrefix, $replacementPrefix) {
40+
/** @var string[] $keys */
41+
$keys = array_keys($data);
42+
43+
/** @var string[] $keys */
44+
$keys = array_map(function(string $el) use ($originalPrefix, $replacementPrefix) {
4145
$el = preg_replace('#^' . $originalPrefix . '#', $replacementPrefix, $el);
4246
return $el;
43-
}, array_keys($data)), array_values($data));
47+
}, $keys);
48+
49+
$fixed = array_combine($keys, array_values($data));
50+
51+
return ($fixed !== false) ? $fixed : [];
4452
}
4553
}

0 commit comments

Comments
 (0)