-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added static method reflection class
- Loading branch information
1 parent
5713a3c
commit 9f45510
Showing
10 changed files
with
206 additions
and
73 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
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
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,72 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JohnstonCode\Reflection\Money; | ||
|
||
use PHPStan\Reflection\ClassMemberReflection; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\FunctionVariant; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\IntegerType; | ||
|
||
class MoneyStaticMethodReflection implements MethodReflection | ||
{ | ||
private $classReflection; | ||
private $name; | ||
|
||
public function __construct(ClassReflection $classReflection, string $name) | ||
{ | ||
$this->classReflection = $classReflection; | ||
$this->name = $name; | ||
} | ||
|
||
public function getDeclaringClass(): ClassReflection | ||
{ | ||
return $this->classReflection; | ||
} | ||
|
||
public function getPrototype(): ClassMemberReflection | ||
{ | ||
return $this; | ||
} | ||
|
||
public function isStatic(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isPrivate(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isPublic(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function isVariadic(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* @return \PHPStan\Reflection\ParametersAcceptor[] | ||
*/ | ||
public function getVariants(): array | ||
{ | ||
return [ | ||
new FunctionVariant( | ||
[new MoneyStaticParameterReflection()], | ||
false, | ||
new ObjectType('Money\Money') | ||
), | ||
]; | ||
} | ||
} |
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,36 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JohnstonCode\Reflection\Money; | ||
|
||
use PHPStan\Reflection\PassedByReference; | ||
use PHPStan\Reflection\ParameterReflection; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\Type; | ||
|
||
class MoneyStaticParameterReflection implements ParameterReflection | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'amount'; | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function getType(): Type | ||
{ | ||
return new IntegerType(); | ||
} | ||
|
||
public function passedByReference(): PassedByReference | ||
{ | ||
return PassedByReference::createNo(); | ||
} | ||
|
||
public function isVariadic(): bool | ||
{ | ||
return false; | ||
} | ||
} |
Empty file.
Empty file.
39 changes: 0 additions & 39 deletions
39
tests/Reflection/Money/MoneyAwareClassReflectionExtensionTest.php
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
tests/Reflection/Money/MoneyMethodsReflectionExtensionTest.php
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,83 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JohnstonCode\Reflection\Money; | ||
|
||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
class MoneyMethodsReflectionExtensionTest extends \PHPStan\Testing\TestCase | ||
{ | ||
private $broker; | ||
private $extension; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->broker = $this->createBroker(); | ||
$this->extension = new MoneyMethodsReflectionExtension(); | ||
} | ||
|
||
public function testHasMethod(): void | ||
{ | ||
$classReflection = $this->broker->getClass('Money\Money'); | ||
$this->assertTrue($this->extension->hasMethod($classReflection, 'getUnits')); | ||
} | ||
|
||
public function testHasStaticMethod(): void | ||
{ | ||
$classReflection = $this->broker->getClass('Money\Money'); | ||
$this->assertTrue($this->extension->hasMethod($classReflection, 'GBP')); | ||
} | ||
|
||
public function testInvalidMethod(): void | ||
{ | ||
$classReflection = $this->broker->getClass('Money\Money'); | ||
$this->assertFalse($this->extension->hasMethod($classReflection, 'ZZZ')); | ||
} | ||
|
||
public function testHasPrivateMethod(): void | ||
{ | ||
$classReflection = $this->broker->getClass('Money\Money'); | ||
$this->assertTrue($this->extension->hasMethod($classReflection, 'assertSameCurrency')); | ||
} | ||
|
||
public function testGetPublicMethod(): void | ||
{ | ||
$classReflection = $this->broker->getClass('Money\Money'); | ||
$methodReflection = $this->extension->getMethod($classReflection, 'getUnits'); | ||
|
||
$this->assertEquals('getUnits', $methodReflection->getName()); | ||
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass()); | ||
$this->assertFalse($methodReflection->isStatic()); | ||
$this->assertFalse($methodReflection->isVariadic()); | ||
$this->assertFalse($methodReflection->isPrivate()); | ||
$this->assertTrue($methodReflection->isPublic()); | ||
} | ||
|
||
public function testGetPrivateMethod(): void | ||
{ | ||
$classReflection = $this->broker->getClass('Money\Money'); | ||
$methodReflection = $this->extension->getMethod($classReflection, 'assertSameCurrency'); | ||
|
||
$this->assertEquals('assertSameCurrency', $methodReflection->getName()); | ||
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass()); | ||
$this->assertFalse($methodReflection->isStatic()); | ||
$this->assertFalse($methodReflection->isVariadic()); | ||
$this->assertTrue($methodReflection->isPrivate()); | ||
$this->assertFalse($methodReflection->isPublic()); | ||
} | ||
|
||
public function testGetStaticMethod(): void | ||
{ | ||
$classReflection = $this->broker->getClass('Money\Money'); | ||
$methodReflection = $this->extension->getMethod($classReflection, 'GBP'); | ||
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); | ||
|
||
$this->assertEquals('GBP', $methodReflection->getName()); | ||
$this->assertEquals($classReflection, $methodReflection->getDeclaringClass()); | ||
$this->assertTrue($methodReflection->isStatic()); | ||
$this->assertFalse($methodReflection->isVariadic()); | ||
$this->assertFalse($methodReflection->isPrivate()); | ||
$this->assertTrue($methodReflection->isPublic()); | ||
$this->assertEquals(\Money\Money::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value())); | ||
} | ||
} |