-
-
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.
[ObjectOriented] Introduce ObjectOriented component
- Loading branch information
Showing
12 changed files
with
349 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\ObjectOriented; | ||
|
||
use function class_alias as php_class_alias; | ||
use function error_reporting; | ||
|
||
/** | ||
* Creates an alias for a class. | ||
* | ||
* Example: | ||
* | ||
* $class_name = Collection\Vector::class; | ||
* $alias = 'AwesomeVector'; | ||
* | ||
* ObjectOriented\class_alias($class_name, $alias); | ||
* => Bool(true) | ||
* | ||
* $vector = new $alias(['a', 'b']); | ||
* | ||
* Type\object($class_name)->matches($vector); | ||
* => Bool(true) | ||
* | ||
* @template T | ||
* | ||
* @param class-string<T> $class_name | ||
* | ||
* @psalm-assert-if-true class-string<T> $alias | ||
* | ||
* @return bool Returns false if unable to create an alias. | ||
*/ | ||
function class_alias(string $class_name, string $alias): bool | ||
{ | ||
$previous = error_reporting(0); | ||
|
||
try { | ||
return php_class_alias($class_name, $alias); | ||
} finally { | ||
error_reporting($previous); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\ObjectOriented; | ||
|
||
use function class_exists as php_class_exists; | ||
|
||
/** | ||
* Checks if the class has been defined. | ||
* | ||
* Example: | ||
* | ||
* ObjectOriented\class_exists('Psl\Collection\Vector'); | ||
* => Bool(true) | ||
* | ||
* ObjectOriented\class_exists('Psl\Collection\Foo'); | ||
* => Bool(false) | ||
* | ||
* @psalm-assert-if-true class-string $class_name | ||
*/ | ||
function class_exists(string $class_name): bool | ||
{ | ||
return php_class_exists($class_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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\ObjectOriented; | ||
|
||
use function get_class as php_get_class; | ||
|
||
/** | ||
* Returns the name of the class of an object. | ||
* | ||
* Example: | ||
* | ||
* ObjectOriented\get_class($vector); | ||
* => Str('Psl\Collection\Vector') | ||
* | ||
* @template T of object | ||
* | ||
* @param T $object | ||
* | ||
* @return class-string<T> | ||
* | ||
* @psalm-pure | ||
*/ | ||
function get_class(object $object): string | ||
{ | ||
return php_get_class($object); | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\ObjectOriented; | ||
|
||
use function interface_exists as php_interface_exists; | ||
|
||
/** | ||
* Checks if the interface has been defined. | ||
* | ||
* Example: | ||
* | ||
* ObjectOriented\interface_exists('Psl\Collection\VectorInterface'); | ||
* => Bool(true) | ||
* | ||
* ObjectOriented\interface_exists('Psl\Collection\FooInterface'); | ||
* => Bool(false) | ||
* | ||
*/ | ||
function interface_exists(string $interface_name): bool | ||
{ | ||
return php_interface_exists($interface_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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\ObjectOriented; | ||
|
||
use function trait_exists as php_trait_exists; | ||
|
||
/** | ||
* Checks if the trait has been defined. | ||
* | ||
* Example: | ||
* | ||
* ObjectOriented\class_exists('Psl\Collection\Vector'); | ||
* => Bool(true) | ||
* | ||
* ObjectOriented\class_exists('Psl\Collection\Foo'); | ||
* => Bool(false) | ||
* | ||
* | ||
* @psalm-assert-if-true class-string $class_name | ||
*/ | ||
function trait_exists(string $trait_name): bool | ||
{ | ||
// PHPs trait_exists returns null in case of an error. | ||
return php_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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\ObjectOriented; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Collection; | ||
use Psl\Iter; | ||
use Psl\ObjectOriented; | ||
use Psl\Type; | ||
|
||
final class ClassAliasTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testClassAlias(bool $expected, object $object, string $alias): void | ||
{ | ||
$class_name = ObjectOriented\get_class($object); | ||
|
||
static::assertSame($expected, ObjectOriented\class_alias($class_name, $alias)); | ||
|
||
if ($expected) { | ||
// Assert alias class exists. | ||
static::assertTrue(ObjectOriented\class_exists($alias)); | ||
|
||
// Assert an instanceof $class_name is also an instanceof $alias | ||
static::assertTrue(Type\object($alias)->matches($object)); | ||
} | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield [ | ||
true, | ||
new Collection\MutableVector([]), | ||
'Psl\Tests\ObjectOriented\Collection\MutableVectorAlias' | ||
]; | ||
|
||
yield [ | ||
true, | ||
Iter\Iterator::create([]), | ||
'Psl\Tests\ObjectOriented\Iter\Iterator' | ||
]; | ||
|
||
yield [ | ||
false, | ||
Iter\Iterator::create([]), | ||
Collection\MutableVector::class | ||
]; | ||
|
||
yield [ | ||
false, | ||
Iter\Iterator::create([]), | ||
Collection\MutableVectorInterface::class | ||
]; | ||
|
||
ObjectOriented\trait_exists(Fixture\EmptyTrait::class); | ||
|
||
yield [ | ||
false, | ||
Iter\Iterator::create([]), | ||
Fixture\EmptyTrait::class | ||
]; | ||
|
||
yield [ | ||
true, | ||
Iter\Iterator::create([]), | ||
'Psl\Str\contains' | ||
]; | ||
} | ||
} |
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\Tests\ObjectOriented; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Collection; | ||
use Psl\ObjectOriented; | ||
|
||
final class ClassExistsTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testClassExists(bool $expected, string $class_name): void | ||
{ | ||
static::assertSame($expected, ObjectOriented\class_exists($class_name)); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield [true, Collection\MutableVector::class]; | ||
yield [false, 'Collection\MutableVector']; | ||
} | ||
} |
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\ObjectOriented\Fixture; | ||
|
||
trait EmptyTrait | ||
{ | ||
} |
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\ObjectOriented; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Collection; | ||
use Psl\Iter; | ||
use Psl\ObjectOriented; | ||
|
||
final class GetClassTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testGetClass(string $expected, object $object): void | ||
{ | ||
static::assertSame($expected, ObjectOriented\get_class($object)); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield [ | ||
Collection\MutableVector::class, | ||
new Collection\MutableVector([]) | ||
]; | ||
|
||
yield [ | ||
Iter\Iterator::class, | ||
Iter\Iterator::create([]), | ||
]; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\ObjectOriented; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Collection; | ||
use Psl\ObjectOriented; | ||
|
||
final class InterfaceExistsTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testInterfaceExists(bool $expected, string $interface_name): void | ||
{ | ||
static::assertSame($expected, ObjectOriented\interface_exists($interface_name)); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield [true, Collection\MutableVectorInterface::class]; | ||
yield [false, Collection\MutableVector::class]; | ||
yield [false, 'Foo\Interface']; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\ObjectOriented; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Collection; | ||
use Psl\ObjectOriented; | ||
|
||
final class TraitExistsTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testTraitExists(bool $expected, string $trait_name): void | ||
{ | ||
static::assertSame($expected, ObjectOriented\trait_exists($trait_name)); | ||
} | ||
|
||
public function provideData(): iterable | ||
{ | ||
yield [true, Fixture\EmptyTrait::class]; | ||
yield [false, Collection\MutableVectorInterface::class]; | ||
yield [false, Collection\MutableVector::class]; | ||
yield [false, 'Foo\Interface']; | ||
} | ||
} |