Skip to content

Commit

Permalink
Dedicated static function to parse out mutator name (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Sep 15, 2020
1 parent ea3c0b4 commit d6bd395
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/Mutator/GetMutatorName.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@

namespace Infection\Mutator;

use function end;
use function explode;

/**
* @internal
*/
trait GetMutatorName
{
final public function getName(): string
{
$parts = explode('\\', static::class);

return (string) end($parts);
return MutatorFactory::getMutatorNameForClassName(static::class);
}
}
9 changes: 9 additions & 0 deletions src/Mutator/MutatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator;

use function end;
use function explode;
use function is_a;
use function Safe\array_flip;
use function Safe\sprintf;
Expand Down Expand Up @@ -95,6 +97,13 @@ public function create(array $resolvedMutators): array
return $mutators;
}

public static function getMutatorNameForClassName(string $className): string
{
$parts = explode('\\', $className);

return (string) end($parts);
}

/**
* @param class-string<ConfigurableMutator> $mutatorClassName
* @param mixed[] $settings
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/Mutator/MutatorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorFactory;
use Infection\Mutator\ProfileList;
use Infection\Mutator\Sort\Spaceship;
use Infection\PhpParser\Visitor\ReflectionVisitor;
use Infection\Reflection\ClassReflection;
use Infection\Tests\SingletonContainer;
Expand Down Expand Up @@ -167,6 +168,13 @@ public function test_it_cannot_create_an_unknown_mutator(): void
}
}

public function test_it_can_parse_name(): void
{
$name = $this->mutatorFactory::getMutatorNameForClassName(Spaceship::class);

$this->assertSame('Spaceship', $name);
}

private function createBoolNode(string $boolean, string $functionName, ClassReflection $reflectionMock): Node
{
return new Node\Expr\ConstFetch(
Expand Down

0 comments on commit d6bd395

Please sign in to comment.