-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from azjezz/fix/infection-and-psalm-config
allow passing infection arguments and configuring psalm config file/directory
- Loading branch information
Showing
5 changed files
with
291 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\InfectionStaticAnalysis; | ||
|
||
use RuntimeException; | ||
|
||
use function array_values; | ||
use function sprintf; | ||
use function strpos; | ||
use function substr; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CliUtility | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @param list<non-empty-string> $arguments | ||
* @param non-empty-string $argument | ||
* | ||
* @return array{0: list<non-empty-string>, 1: (non-empty-string|null)} | ||
*/ | ||
public static function extractArgument(array $arguments, string $argument): array | ||
{ | ||
$lookup = '--' . $argument; | ||
|
||
$result = null; | ||
$present = false; | ||
foreach ($arguments as $index => $arg) { | ||
if ($arg === $lookup) { | ||
$present = true; | ||
unset($arguments[$index]); | ||
// grab the next argument in the list | ||
$value = $arguments[$index + 1] ?? null; | ||
// if the argument is not a flag/argument name ( starts with - ) | ||
if ($value !== null && strpos($value, '-') !== 0) { | ||
// consider it the value, and remove it from the list. | ||
$result = $value; | ||
unset($arguments[$index + 1]); | ||
} | ||
|
||
break; | ||
} | ||
|
||
// if the argument starts with `--argument-name=` | ||
// we consider anything after '=' to be the value | ||
if (strpos($arg, $lookup . '=') === 0) { | ||
$present = true; | ||
unset($arguments[$index]); | ||
|
||
$result = substr($arg, 15); | ||
break; | ||
} | ||
} | ||
|
||
$arguments = array_values($arguments); | ||
$value = self::removeSurroundingQuites($result); | ||
if ($present && $value === null) { | ||
throw new RuntimeException(sprintf('Please provide a value for "%s" argument.', $argument)); | ||
} | ||
|
||
return [$arguments, $value]; | ||
} | ||
|
||
/** | ||
* @return non-empty-string|null | ||
*/ | ||
private static function removeSurroundingQuites(?string $argument): ?string | ||
{ | ||
if ($argument === null || $argument === '') { | ||
return null; | ||
} | ||
|
||
if ($argument[0] === '"') { | ||
$argument = substr($argument, 1); | ||
} | ||
|
||
if (substr($argument, -1) === '"') { | ||
$argument = substr($argument, 0, -1); | ||
} | ||
|
||
return $argument === '' ? null : $argument; | ||
} | ||
} |
170 changes: 170 additions & 0 deletions
170
test/unit/Roave/InfectionStaticAnalysisTest/CliUtilityTest.php
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,170 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\InfectionStaticAnalysisTest; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Roave\InfectionStaticAnalysis\CliUtility; | ||
use RuntimeException; | ||
|
||
use function sprintf; | ||
|
||
/** | ||
* @covers \Roave\InfectionStaticAnalysis\CliUtility | ||
*/ | ||
final class CliUtilityTest extends TestCase | ||
{ | ||
/** | ||
* @param list<non-empty-string> $expectedNewArguments | ||
* @param non-empty-string|null $expectedArgumentValue | ||
* @param list<non-empty-string> $arguments | ||
* @param non-empty-string $argument | ||
* | ||
* @dataProvider provideExtractionData | ||
*/ | ||
public function testExtractArgument( | ||
array $expectedNewArguments, | ||
?string $expectedArgumentValue, | ||
array $arguments, | ||
string $argument | ||
): void { | ||
$result = CliUtility::extractArgument($arguments, $argument); | ||
|
||
self::assertSame($expectedNewArguments, $result[0]); | ||
self::assertSame($expectedArgumentValue, $result[1]); | ||
} | ||
|
||
/** | ||
* @return list<array{ | ||
* 0: list<non-empty-string>, | ||
* 1: (non-empty-string|null), | ||
* 2: list<non-empty-string>, | ||
* 3: non-empty-string | ||
* }> | ||
*/ | ||
public function provideExtractionData(): array | ||
{ | ||
return [ | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config', 'configuration/psalm.xml'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config', '"configuration/psalm.xml"'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config="configuration/psalm.xml"'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config=configuration/psalm.xml"'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config="configuration/psalm.xml'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config=configuration/psalm.xml'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config=foo'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config=configuration/psalm.xml', '--psalm-config=foo'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config=foo'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config', 'configuration/psalm.xml', '--psalm-config=foo'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config', 'foo'], | ||
'configuration/psalm.xml', | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config=configuration/psalm.xml', '--psalm-config', 'foo'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin'], | ||
null, | ||
['vendor/bin/roave-infection-static-analysis-plugin'], | ||
'psalm-config', | ||
], | ||
[ | ||
[], | ||
null, | ||
[], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config', 'configuration/psalm.xml'], | ||
null, | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config', 'configuration/psalm.xml'], | ||
'psalm', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config', 'configuration/psalm.xml'], | ||
null, | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config', 'configuration/psalm.xml'], | ||
'psalm', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param list<non-empty-string> $arguments | ||
* @param non-empty-string $argument | ||
* | ||
* @dataProvider provideExtractionMissingValueData | ||
*/ | ||
public function testExtractArgumentThrowsForMissingValue( | ||
array $arguments, | ||
string $argument | ||
): void { | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage(sprintf('Please provide a value for "%s" argument.', $argument)); | ||
|
||
CliUtility::extractArgument($arguments, $argument); | ||
} | ||
|
||
/** | ||
* @return list<array{0: list<non-empty-string>, 1: non-empty-string}> | ||
*/ | ||
public function provideExtractionMissingValueData(): array | ||
{ | ||
return [ | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config='], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config=""'], | ||
'psalm-config', | ||
], | ||
[ | ||
['vendor/bin/roave-infection-static-analysis-plugin', '--psalm-config="'], | ||
'psalm-config', | ||
], | ||
]; | ||
} | ||
} |