-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Class, Trait, and Interface components
- Loading branch information
Showing
16 changed files
with
367 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Class; | ||
|
||
use function class_exists; | ||
|
||
/** | ||
* Checks if the class with the given name has already been defined. | ||
* | ||
* @param string $class_name | ||
* | ||
* @psalm-assert-if-true class-string $classname | ||
* | ||
* @pure | ||
*/ | ||
function defined(string $class_name): bool | ||
{ | ||
/** | ||
* @psalm-suppress ImpureFunctionCall - call is pure. | ||
* | ||
* @var bool | ||
*/ | ||
return class_exists($class_name, false); | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Class; | ||
|
||
use function class_exists; | ||
|
||
/** | ||
* Checks if the class with the given name exists. | ||
* | ||
* @param string $class_name | ||
* | ||
* @psalm-assert-if-true class-string $classname | ||
*/ | ||
function exists(string $class_name): bool | ||
{ | ||
/** @var bool */ | ||
return class_exists($class_name, true); | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Class; | ||
|
||
use Psl; | ||
use ReflectionClass; | ||
|
||
/** | ||
* Checks if constant is defined in the given class. | ||
* | ||
* @param class-string $class_name | ||
* | ||
* @throws Psl\Exception\InvariantViolationException If $class_name does not exist. | ||
*/ | ||
function has_constant(string $class_name, string $constant_name): bool | ||
{ | ||
Psl\invariant(namespace\exists($class_name), 'Classname "%s" does not exist.', $class_name); | ||
|
||
/** @psalm-suppress MissingThrowsDocblock */ | ||
return (new ReflectionClass($class_name))->hasConstant($constant_name); | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Class; | ||
|
||
use Psl; | ||
use ReflectionClass; | ||
|
||
/** | ||
* Checks if method is defined in the given class. | ||
* | ||
* @param class-string $class_name | ||
* | ||
* @throws Psl\Exception\InvariantViolationException If $class_name does not exist. | ||
*/ | ||
function has_method(string $class_name, string $method_name): bool | ||
{ | ||
Psl\invariant(namespace\exists($class_name), 'Classname "%s" does not exist.', $class_name); | ||
|
||
/** @psalm-suppress MissingThrowsDocblock */ | ||
return (new ReflectionClass($class_name))->hasMethod($method_name); | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Class; | ||
|
||
use Psl; | ||
use ReflectionClass; | ||
|
||
/** | ||
* Checks if class is abstract. | ||
* | ||
* @param class-string $class_name | ||
* | ||
* @throws Psl\Exception\InvariantViolationException If $class_name does not exist. | ||
*/ | ||
function is_abstract(string $class_name): bool | ||
{ | ||
Psl\invariant(namespace\exists($class_name), 'Classname "%s" does not exist.', $class_name); | ||
|
||
/** @psalm-suppress MissingThrowsDocblock */ | ||
return (new ReflectionClass($class_name))->isAbstract(); | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Class; | ||
|
||
use Psl; | ||
use ReflectionClass; | ||
|
||
/** | ||
* Checks if class is final. | ||
* | ||
* @param class-string $class_name | ||
* | ||
* @throws Psl\Exception\InvariantViolationException If $class_name does not exist. | ||
*/ | ||
function is_final(string $class_name): bool | ||
{ | ||
Psl\invariant(namespace\exists($class_name), 'Classname "%s" does not exist.', $class_name); | ||
|
||
/** @psalm-suppress MissingThrowsDocblock */ | ||
return (new ReflectionClass($class_name))->isFinal(); | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Interface; | ||
|
||
use function interface_exists; | ||
|
||
/** | ||
* Checks if the interface with the given name has already been defined. | ||
* | ||
* @param string $interface_name | ||
* | ||
* @pure | ||
*/ | ||
function defined(string $interface_name): bool | ||
{ | ||
/** @var bool */ | ||
return interface_exists($interface_name, false); | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Interface; | ||
|
||
use function interface_exists; | ||
|
||
/** | ||
* Checks if the interface with the given name exists. | ||
* | ||
* @param string $interface_name | ||
*/ | ||
function exists(string $interface_name): bool | ||
{ | ||
/** @var bool */ | ||
return interface_exists($interface_name, true); | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Trait; | ||
|
||
use function trait_exists; | ||
|
||
/** | ||
* Checks if the trait with the given name has already been defined. | ||
* | ||
* @param string $trait_name | ||
* | ||
* @pure | ||
*/ | ||
function defined(string $trait_name): bool | ||
{ | ||
/** @var bool */ | ||
return trait_exists($trait_name, false); | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Trait; | ||
|
||
use function trait_exists; | ||
|
||
/** | ||
* Checks if the trait with the given name exists. | ||
* | ||
* @param string $trait_name | ||
*/ | ||
function exists(string $trait_name): bool | ||
{ | ||
/** @var bool */ | ||
return trait_exists($trait_name, true); | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Fixture; | ||
|
||
trait ExampleTrait | ||
{ | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Class; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Class; | ||
use Psl\Collection; | ||
use Psl\Type; | ||
|
||
final class ClassTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function test( | ||
string $classname, | ||
bool $exists, | ||
bool $final, | ||
bool $abstract, | ||
array $methods = [], | ||
array $constants = [] | ||
): void { | ||
static::assertSame($exists, Class\exists($classname)); | ||
static::assertSame($exists, Class\defined($classname)); | ||
if (!$exists) { | ||
return; | ||
} | ||
|
||
static::assertSame($final, Class\is_final($classname)); | ||
static::assertSame($abstract, Class\is_abstract($classname)); | ||
|
||
foreach ($methods as $method) { | ||
static::assertTrue(CLass\has_method($classname, $method)); | ||
} | ||
|
||
static::assertFalse(Class\has_method($classname, 'ImNotAClassMethod')); | ||
|
||
foreach ($constants as $constant) { | ||
static::assertTrue(Class\has_constant($classname, $constant)); | ||
} | ||
|
||
static::assertFalse(Class\has_constant($classname, 'I_AM_NOT_A_CONSTANT')); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield [Collection\Vector::class, true, true, false, ['first', 'last'], []]; | ||
yield [Collection\MutableVector::class, true, true, false, ['first', 'last'], []]; | ||
yield [Collection\Map::class, true, true, false, ['first', 'last'], []]; | ||
yield [Collection\MutableMap::class, true, true, false, ['first', 'last'], []]; | ||
yield [Type\Type::class, true, false, true, ['matches', 'getTrace', 'withTrace', 'isOptional'], []]; | ||
|
||
yield ['Psl\\Not\\Class', false, false, false, [], []]; | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Interface; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Collection; | ||
use Psl\Interface; | ||
use Psl\Type; | ||
|
||
final class InterfaceTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function test( | ||
string $interface_name, | ||
bool $exists, | ||
): void { | ||
static::assertSame($exists, Interface\exists($interface_name)); | ||
static::assertSame($exists, Interface\defined($interface_name)); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield [Collection\VectorInterface::class, true]; | ||
yield [Collection\MutableVectorInterface::class, true]; | ||
yield [Collection\MapInterface::class, true]; | ||
yield [Collection\MutableMapInterface::class, true]; | ||
yield [Type\TypeInterface::class, true]; | ||
|
||
yield [Collection\Vector::class, false]; | ||
yield [Collection\MutableVector::class, false]; | ||
yield [Collection\Map::class, false]; | ||
yield [Collection\MutableMap::class, false]; | ||
yield [Type\Type::class, false]; | ||
|
||
yield ['Psl\\Not\\Interface', false]; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Trait; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Tests\Fixture; | ||
use Psl\Trait; | ||
|
||
final class TraitTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function test( | ||
string $trait_name, | ||
bool $defined, | ||
bool $exists, | ||
): void { | ||
static::assertSame($defined, Trait\defined($trait_name)); | ||
static::assertSame($exists, Trait\exists($trait_name)); | ||
|
||
if ($exists) { | ||
static::assertTrue(Trait\defined($trait_name)); | ||
} | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield [Fixture\ExampleTrait::class, false, true]; | ||
yield ['Psl\\Not\\Trait', false, false]; | ||
} | ||
} |