Skip to content

Commit

Permalink
Let CodeUnit::fromString() return a collection of CodeUnit objects in…
Browse files Browse the repository at this point in the history
…stead of a CodeUnit object (even if only a single CodeUnit object is to be returned)

The purpose of this component is to help with refactoring how information from @Covers and @uses annotations is passed from phpunit/phpunit to phpunit/php-code-coverage, see sebastianbergmann/php-code-coverage#519 for details.

I want this to happen sooner rather than later and do not want to wait for PHPUnit 10 and sebastianbergmann/phpunit#3631.

This means that, at least until PHPUnit 10, this component needs to support these strings for CodeUnit::fromString():

* ClassName::<public>
* ClassName::<protected>
* ClassName::<private>
* ClassName::<!public>
* ClassName::<!protected>
* ClassName::<!private>
  • Loading branch information
sebastianbergmann committed Mar 1, 2020
1 parent 2e1120c commit 470b4eb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 22 deletions.
16 changes: 8 additions & 8 deletions src/CodeUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,37 +195,37 @@ public static function forFunction(string $functionName): FunctionUnit
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public static function fromString(string $unit): self
public static function fromString(string $unit): CodeUnitCollection
{
if (\strpos($unit, '::') !== false) {
[$type, $method] = \explode('::', $unit);

if (\class_exists($type)) {
return self::forClassMethod($type, $method);
return CodeUnitCollection::fromList(self::forClassMethod($type, $method));
}

if (\interface_exists($type)) {
return self::forInterfaceMethod($type, $method);
return CodeUnitCollection::fromList(self::forInterfaceMethod($type, $method));
}

if (\trait_exists($type)) {
return self::forTraitMethod($type, $method);
return CodeUnitCollection::fromList(self::forTraitMethod($type, $method));
}
} else {
if (\class_exists($unit)) {
return self::forClass($unit);
return CodeUnitCollection::fromList(self::forClass($unit));
}

if (\interface_exists($unit)) {
return self::forInterface($unit);
return CodeUnitCollection::fromList(self::forInterface($unit));
}

if (\trait_exists($unit)) {
return self::forTrait($unit);
return CodeUnitCollection::fromList(self::forTrait($unit));
}

if (\function_exists($unit)) {
return self::forFunction($unit);
return CodeUnitCollection::fromList(self::forFunction($unit));
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/unit/ClassMethodUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @covers \SebastianBergmann\CodeUnit\ClassMethodUnit
* @covers \SebastianBergmann\CodeUnit\CodeUnit
*
* @uses \SebastianBergmann\CodeUnit\CodeUnitCollection
*
* @testdox ClassMethodUnit
*/
final class ClassMethodUnitTest extends TestCase
Expand All @@ -41,9 +43,9 @@ public function testCanBeCreatedFromNameOfUserDefinedClassAndMethodName(): void

public function testCanBeCreatedFromString(): void
{
$unit = CodeUnit::fromString(FixtureClass::class . '::method');
$units = CodeUnit::fromString(FixtureClass::class . '::method');

$this->assertSame(FixtureClass::class . '::method', $unit->name());
$this->assertSame(FixtureClass::class . '::method', $units->asArray()[0]->name());
}

public function testCannotBeCreatedForMethodOfInternalClass(): void
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/ClassUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @covers \SebastianBergmann\CodeUnit\ClassUnit
* @covers \SebastianBergmann\CodeUnit\CodeUnit
*
* @uses \SebastianBergmann\CodeUnit\CodeUnitCollection
*
* @testdox ClassUnit
*/
final class ClassUnitTest extends TestCase
Expand All @@ -41,9 +43,9 @@ public function testCanBeCreatedFromNameOfUserDefinedClass(): void

public function testCanBeCreatedFromString(): void
{
$unit = CodeUnit::fromString(FixtureClass::class);
$units = CodeUnit::fromString(FixtureClass::class);

$this->assertSame(FixtureClass::class, $unit->name());
$this->assertSame(FixtureClass::class, $units->asArray()[0]->name());
}

public function testCannotBeCreatedForInternalClass(): void
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/FunctionUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @covers \SebastianBergmann\CodeUnit\FunctionUnit
* @covers \SebastianBergmann\CodeUnit\CodeUnit
*
* @uses \SebastianBergmann\CodeUnit\CodeUnitCollection
*
* @testdox FunctionUnit
*/
final class FunctionUnitTest extends TestCase
Expand All @@ -38,9 +40,9 @@ public function testCanBeCreatedFromNameOfUserFunction(): void

public function testCanBeCreatedFromString(): void
{
$unit = CodeUnit::fromString('SebastianBergmann\CodeUnit\Fixture\f');
$units = CodeUnit::fromString('SebastianBergmann\CodeUnit\Fixture\f');

$this->assertSame('SebastianBergmann\CodeUnit\Fixture\f', $unit->name());
$this->assertSame('SebastianBergmann\CodeUnit\Fixture\f', $units->asArray()[0]->name());
}

public function testCannotBeCreatedForInternalFunction(): void
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/InterfaceMethodUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @covers \SebastianBergmann\CodeUnit\InterfaceMethodUnit
* @covers \SebastianBergmann\CodeUnit\CodeUnit
*
* @uses \SebastianBergmann\CodeUnit\CodeUnitCollection
*
* @testdox InterfaceMethodUnit
*/
final class InterfaceMethodUnitTest extends TestCase
Expand All @@ -41,9 +43,9 @@ public function testCanBeCreatedFromNameOfUserDefinedInterfaceAndMethodName(): v

public function testCanBeCreatedFromString(): void
{
$unit = CodeUnit::fromString(FixtureInterface::class . '::method');
$units = CodeUnit::fromString(FixtureInterface::class . '::method');

$this->assertSame(FixtureInterface::class . '::method', $unit->name());
$this->assertSame(FixtureInterface::class . '::method', $units->asArray()[0]->name());
}

public function testCannotBeCreatedForMethodOfInternalInterface(): void
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/InterfaceUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @covers \SebastianBergmann\CodeUnit\InterfaceUnit
* @covers \SebastianBergmann\CodeUnit\CodeUnit
*
* @uses \SebastianBergmann\CodeUnit\CodeUnitCollection
*
* @testdox InterfaceUnit
*/
final class InterfaceUnitTest extends TestCase
Expand All @@ -41,9 +43,9 @@ public function testCanBeCreatedFromNameOfUserDefinedInterface(): void

public function testCanBeCreatedFromString(): void
{
$unit = CodeUnit::fromString(FixtureInterface::class);
$units = CodeUnit::fromString(FixtureInterface::class);

$this->assertSame(FixtureInterface::class, $unit->name());
$this->assertSame(FixtureInterface::class, $units->asArray()[0]->name());
}

public function testCannotBeCreatedForInternalInterface(): void
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/TraitMethodUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @covers \SebastianBergmann\CodeUnit\TraitMethodUnit
* @covers \SebastianBergmann\CodeUnit\CodeUnit
*
* @uses \SebastianBergmann\CodeUnit\CodeUnitCollection
*
* @testdox TraitMethodUnit
*/
final class TraitMethodUnitTest extends TestCase
Expand All @@ -41,9 +43,9 @@ public function testCanBeCreatedFromNameOfUserDefinedTraitAndMethodName(): void

public function testCanBeCreatedFromString(): void
{
$unit = CodeUnit::fromString(FixtureTrait::class . '::method');
$units = CodeUnit::fromString(FixtureTrait::class . '::method');

$this->assertSame(FixtureTrait::class . '::method', $unit->name());
$this->assertSame(FixtureTrait::class . '::method', $units->asArray()[0]->name());
}

public function testCannotBeCreatedForClassMethod(): void
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/TraitUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @covers \SebastianBergmann\CodeUnit\TraitUnit
* @covers \SebastianBergmann\CodeUnit\CodeUnit
*
* @uses \SebastianBergmann\CodeUnit\CodeUnitCollection
*
* @testdox TraitUnit
*/
final class TraitUnitTest extends TestCase
Expand All @@ -41,9 +43,9 @@ public function testCanBeCreatedFromNameOfUserDefinedTrait(): void

public function testCanBeCreatedFromString(): void
{
$unit = CodeUnit::fromString(FixtureTrait::class);
$units = CodeUnit::fromString(FixtureTrait::class);

$this->assertSame(FixtureTrait::class, $unit->name());
$this->assertSame(FixtureTrait::class, $units->asArray()[0]->name());
}

public function testCannotBeCreatedForClass(): void
Expand Down

0 comments on commit 470b4eb

Please sign in to comment.