Skip to content

100 % mutation score #1320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Better Reflection
=================

[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FRoave%2FBetterReflection%2F6.0.x)](https://dashboard.stryker-mutator.io/reports/github.com/Roave/BetterReflection/6.0.x)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FRoave%2FBetterReflection%2F6.6.x)](https://dashboard.stryker-mutator.io/reports/github.com/Roave/BetterReflection/6.6.x)
[![Type Coverage](https://shepherd.dev/github/Roave/BetterReflection/coverage.svg)](https://shepherd.dev/github/Roave/BetterReflection)
[![Latest Stable Version](https://poser.pugx.org/roave/better-reflection/v/stable)](https://packagist.org/packages/roave/better-reflection)
[![License](https://poser.pugx.org/roave/better-reflection/license)](https://packagist.org/packages/roave/better-reflection)
Expand Down
277 changes: 140 additions & 137 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"mutators": {
"@default": true
},
"minMsi": 99,
"minMsi": 100,
"minCoveredMsi": 100
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function withStreamWrapperOverride(
$result = $executeMeWithinStreamWrapperOverride();
} finally {
foreach ($streamWrapperProtocols as $protocol) {
stream_wrapper_restore($protocol);
@stream_wrapper_restore($protocol);
}

self::$registeredStreamWrapperProtocols = null;
Expand Down Expand Up @@ -101,6 +101,7 @@ public function stream_open($path, $mode, $options, &$opened_path): bool
{
self::$autoloadLocatedFile = $path;

// @infection-ignore-all FalseValue
return false;
}

Expand Down Expand Up @@ -130,7 +131,7 @@ public function url_stat($path, $flags): array|bool
stream_wrapper_restore($protocol);
}

if ($flags & STREAM_URL_STAT_QUIET) {
if (($flags & STREAM_URL_STAT_QUIET) === STREAM_URL_STAT_QUIET) {
$result = @stat($path);
} else {
$result = stat($path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use function is_array;
use function is_dir;
use function is_file;
use function is_string;
use function json_decode;
use function realpath;

Expand Down Expand Up @@ -61,8 +62,11 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
throw MissingComposerJson::inProjectPath($installationPath);
}

$composerJsonContent = file_get_contents($composerJsonPath);
assert(is_string($composerJsonContent));

/** @psalm-var array{autoload: ComposerAutoload}|null $composer */
$composer = json_decode((string) file_get_contents($composerJsonPath), true);
$composer = json_decode($composerJsonContent, true);

if (! is_array($composer)) {
throw FailedToParseJson::inFile($composerJsonPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use function is_array;
use function is_dir;
use function is_file;
use function is_string;
use function json_decode;
use function realpath;
use function rtrim;
Expand All @@ -52,8 +53,11 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
throw MissingComposerJson::inProjectPath($installationPath);
}

$composerJsonContent = file_get_contents($composerJsonPath);
assert(is_string($composerJsonContent));

/** @psalm-var Composer|null $composer */
$composer = json_decode((string) file_get_contents($composerJsonPath), true);
$composer = json_decode($composerJsonContent, true);
$vendorDir = $composer['config']['vendor-dir'] ?? 'vendor';
$vendorDir = rtrim($vendorDir, '/');

Expand All @@ -63,8 +67,11 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
throw MissingInstalledJson::inProjectPath($realInstallationPath . '/' . $vendorDir);
}

$jsonContent = file_get_contents($installedJsonPath);
assert(is_string($jsonContent));

/** @psalm-var array{packages: list<mixed[]>}|list<mixed[]>|null $installedJson */
$installedJson = json_decode((string) file_get_contents($installedJsonPath), true);
$installedJson = json_decode($jsonContent, true);

if (! is_array($composer)) {
throw FailedToParseJson::inFile($composerJsonPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use function is_array;
use function is_dir;
use function is_file;
use function is_string;
use function json_decode;
use function realpath;
use function rtrim;
Expand All @@ -52,8 +53,11 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
throw MissingComposerJson::inProjectPath($installationPath);
}

$composerJsonContent = file_get_contents($composerJsonPath);
assert(is_string($composerJsonContent));

/** @psalm-var Composer|null $composer */
$composer = json_decode((string) file_get_contents($composerJsonPath), true);
$composer = json_decode($composerJsonContent, true);
$vendorDir = $composer['config']['vendor-dir'] ?? 'vendor';
$vendorDir = rtrim($vendorDir, '/');

Expand All @@ -63,8 +67,11 @@ public function __invoke(string $installationPath, Locator $astLocator): SourceL
throw MissingInstalledJson::inProjectPath($realInstallationPath . '/' . $vendorDir);
}

$jsonContent = file_get_contents($installedJsonPath);
assert(is_string($jsonContent));

/** @var array{packages: list<mixed[]>}|list<mixed[]>|null $installedJson */
$installedJson = json_decode((string) file_get_contents($installedJsonPath), true);
$installedJson = json_decode($jsonContent, true);

if (! is_array($installedJson)) {
throw FailedToParseJson::inFile($installedJsonPath);
Expand Down
1 change: 1 addition & 0 deletions src/SourceLocator/Type/FileIteratorSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function __construct(Iterator $fileInfoIterator, private Locator $astLoca
/** @throws InvalidFileLocation */
private function getAggregatedSourceLocator(): AggregateSourceLocator
{
// @infection-ignore-all Coalesce: There's no difference, it's just optimization
return $this->aggregateSourceLocator
?? $this->aggregateSourceLocator = new AggregateSourceLocator(array_values(array_filter(array_map(
function (SplFileInfo $item): SingleFileSourceLocator|null {
Expand Down
4 changes: 3 additions & 1 deletion src/Util/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use function assert;
use function preg_match;
use function sprintf;
use function str_replace;

use const DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -43,13 +44,14 @@ public static function normalizeSystemPath(string $originalPath): string
[, $scheme, $path] = $matches;
}

// @infection-ignore-all Identical Needed only on Windows
if (DIRECTORY_SEPARATOR === '\\') {
// @infection-ignore-all UnwrapStrReplace Needed only on Windows
$path = str_replace('/', DIRECTORY_SEPARATOR, $path);
}

assert($path !== '');

return ($scheme !== null ? $scheme . '://' : '') . $path;
return ($scheme !== null ? sprintf('%s://', $scheme) : '') . $path;
}
}
2 changes: 1 addition & 1 deletion test/unit/Fixture/InitializedProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public function __construct()

public function __get($property)
{
throw new Error();
throw new Error('Removed property');
}
}
1 change: 1 addition & 0 deletions test/unit/Reflection/ReflectionPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ public function testIsInitializedThrowsError(): void
$classReflection = $this->reflector->reflectClass(InitializedProperties::class);

self::expectException(Error::class);
self::expectExceptionMessage('Removed property');
$classReflection->getProperty('toBeRemoved')->isInitialized($object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@

use function file_get_contents;
use function is_file;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use function uniqid;

use const E_WARNING;

/**
* @covers \Roave\BetterReflection\SourceLocator\Type\AutoloadSourceLocator\FileReadTrapStreamWrapper
*
Expand Down Expand Up @@ -177,28 +181,38 @@ static function () use ($thrown): string {

public function testWillRaiseWarningWhenTryingToCheckFileExistenceForNonExistingFileWithoutSilencingModifier(): void
{
set_error_handler(static function (int $errno, string $errstr): bool {
throw new Exception($errstr, $errno);
}, E_WARNING);

$nonExistingFile = __DIR__ . uniqid('non-existing-file', true);

self::assertSame(
'another value produced by the function',
FileReadTrapStreamWrapper::withStreamWrapperOverride(
static function () use ($nonExistingFile): string {
if (is_file($nonExistingFile)) {
throw new UnexpectedValueException('is_file() should report `false` for a non-existing file');
}
self::expectExceptionMessageMatches('~stat\(\): stat failed for~');

if (@file_get_contents($nonExistingFile) !== false) {
throw new UnexpectedValueException('file_get_contents() should report `false` for a non-existing file');
}
try {
self::assertSame(
'another value produced by the function',
FileReadTrapStreamWrapper::withStreamWrapperOverride(
static function () use ($nonExistingFile): string {
if (is_file($nonExistingFile)) {
throw new UnexpectedValueException('is_file() should report `false` for a non-existing file');
}

return 'another value produced by the function';
},
['file'],
),
);
if (file_get_contents($nonExistingFile) !== false) {
throw new UnexpectedValueException('file_get_contents() should report `false` for a non-existing file');
}

self::assertNull(FileReadTrapStreamWrapper::$autoloadLocatedFile);
self::assertNotEmpty(file_get_contents(__FILE__), 'Stream wrapper was removed, file reads work again');
return 'another value produced by the function';
},
['file'],
),
);
} finally {
self::assertNull(FileReadTrapStreamWrapper::$autoloadLocatedFile);
self::assertNotEmpty(file_get_contents(__FILE__), 'Stream wrapper was removed, file reads work again');

restore_error_handler();
}
}

public function testUrlStatThrowsExceptionWhenCalledDirectly(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public function testWillFailToProduceLocatorForProjectWithoutComposerJson(): voi
public function testWillFailToProduceLocatorForProjectWithoutInstalledJson(): void
{
$this->expectException(MissingInstalledJson::class);
$this->expectExceptionMessageMatches('~^Could not locate a "composer/installed.json" file in "[^"]+[\\\\/]Assets[\\\\/]ComposerLocators[\\\\/]project-without-installed.json/vendor"$~');

(new MakeLocatorForInstalledJson())
->__invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public function testWillFailToProduceLocatorForProjectWithoutComposerJson(): voi
public function testWillFailToProduceLocatorForProjectWithoutInstalledJson(): void
{
$this->expectException(MissingInstalledJson::class);
$this->expectExceptionMessageMatches('~^Could not locate a "composer/installed.json" file in "[^"]+[\\\\/]Assets[\\\\/]ComposerLocators[\\\\/]project-without-installed.json/vendor"$~');

(new MakeLocatorForComposerJsonAndInstalledJson())
->__invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function setUp(): void
new RecursiveIteratorIterator(new RecursiveDirectoryIterator(
__DIR__ . '/../../Assets/DirectoryScannerAssets',
RecursiveDirectoryIterator::SKIP_DOTS,
)),
), RecursiveIteratorIterator::SELF_FIRST),
BetterReflectionSingleton::instance()->astLocator(),
);
}
Expand Down
30 changes: 18 additions & 12 deletions test/unit/Util/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
use PHPUnit\Framework\TestCase;
use Roave\BetterReflection\Util\FileHelper;

use function strtr;

use const DIRECTORY_SEPARATOR;

/** @covers \Roave\BetterReflection\Util\FileHelper */
class FileHelperTest extends TestCase
{
Expand All @@ -20,19 +16,29 @@ public function testNormalizeWindowsPath(): void
self::assertSame('directory/foo/boo/file.php', FileHelper::normalizeWindowsPath('directory/foo/boo/file.php'));
}

public function testSystemWindowsPath(): void
/** @return list<array{0: string, 1: string}> */
public function dataNormalizeSystemPath(): array
{
$path = 'directory\\foo/boo\\foo/file.php';

self::assertSame(strtr($path, '\\/', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR), FileHelper::normalizeSystemPath($path));
return [
['directory\\foo/boo\\foo/file.php', 'directory/foo/boo/foo/file.php'],
['phar://C:/Users/ondrej/phpstan.phar/src/TrinaryLogic.php', 'phar://C:/Users/ondrej/phpstan.phar/src/TrinaryLogic.php'],
['C:/Users/ondrej/phpstan.phar/src/TrinaryLogic.php', 'C:/Users/ondrej/phpstan.phar/src/TrinaryLogic.php'],
['/directory/strange-path-c://file.php', '/directory/strange-path-c://file.php'],
];
}

public function testSystemWindowsPathWithProtocol(): void
/**
* @dataProvider dataNormalizeSystemPath
* @requires OS Linux
*/
public function testSystemWindowsPath(string $path, string $expectedPath): void
{
if (DIRECTORY_SEPARATOR !== '\\') {
$this->markTestSkipped('Test runs only on Windows');
}
self::assertSame($expectedPath, FileHelper::normalizeSystemPath($path));
}

/** @requires OSFAMILY Windows */
public function testSystemWindowsPathOnWindows(): void
{
$path = 'phar://C:/Users/ondrej/phpstan.phar/src/TrinaryLogic.php';
self::assertSame(
'phar://C:\Users\ondrej\phpstan.phar\src\TrinaryLogic.php',
Expand Down
Loading