-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for constant expression typehints (#65)
* Add support for constant expression typehints Fixing #39
- Loading branch information
Showing
11 changed files
with
235 additions
and
17 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
54 changes: 54 additions & 0 deletions
54
src/Constraint/ValueProvider/Pseudo/ConstExpressionProvider.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,54 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Pseudo; | ||
|
||
use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProvider; | ||
use phpDocumentor\Reflection\Type; | ||
use phpDocumentor\Reflection\Types\Object_; | ||
use phpDocumentor\Reflection\Types\Self_; | ||
use ReflectionClass; | ||
use ReflectionMethod; | ||
use RuntimeException; | ||
|
||
class ConstExpressionProvider implements ValueProvider | ||
{ | ||
protected Type $owner; | ||
protected string $expression; | ||
protected ?ReflectionMethod $method; | ||
|
||
public function __construct(Type $owner, string $expression, ?ReflectionMethod $method) | ||
{ | ||
$this->owner = $owner; | ||
$this->expression = $expression; | ||
$this->method = $method; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getValues(): array | ||
{ | ||
if ($this->owner instanceof Object_ && $this->owner->getFqsen() !== null) { | ||
/** @var class-string $fqsen */ | ||
$fqsen = (string)$this->owner->getFqsen(); | ||
|
||
$constClass = new ReflectionClass($fqsen); | ||
} elseif ($this->owner instanceof Self_ && $this->method !== null) { | ||
$constClass = $this->method->getDeclaringClass(); | ||
} else { | ||
throw new RuntimeException('ConstExpressionProvider can only be used with object or self typehints'); | ||
} | ||
|
||
$constants = $constClass->getConstants(); | ||
$expression = str_replace('*', '.*', $this->expression); | ||
$values = []; | ||
foreach ($constants as $constant => $value) { | ||
if (preg_match('/^' . $expression . '$/', $constant) === 1) { | ||
$values[] = $value; | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
tests/Integration/data/success/Regular/ConstOtherProperty.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,28 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DigitalRevolution\AccessorPairConstraint\Tests\Integration\data\success\Regular; | ||
|
||
class ConstOtherProperty | ||
{ | ||
/** @var ConstSelfProperty::CONSTANT_* */ | ||
private string $property; | ||
|
||
/** | ||
* @return ConstSelfProperty::CONSTANT_* | ||
*/ | ||
public function getProperty(): string | ||
{ | ||
return $this->property; | ||
} | ||
|
||
/** | ||
* @param ConstSelfProperty::CONSTANT_* $property | ||
*/ | ||
public function setProperty(string $property): self | ||
{ | ||
$this->property = $property; | ||
|
||
return $this; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/Integration/data/success/Regular/ConstSelfProperty.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,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DigitalRevolution\AccessorPairConstraint\Tests\Integration\data\success\Regular; | ||
|
||
class ConstSelfProperty | ||
{ | ||
public const CONSTANT_1 = 'value1'; | ||
public const CONSTANT_2 = 'value2'; | ||
|
||
/** @var self::CONSTANT_* */ | ||
private string $property; | ||
|
||
/** | ||
* @return self::CONSTANT_* | ||
*/ | ||
public function getProperty(): string | ||
{ | ||
return $this->property; | ||
} | ||
|
||
/** | ||
* @param self::CONSTANT_* $property | ||
*/ | ||
public function setProperty(string $property): self | ||
{ | ||
$this->property = $property; | ||
|
||
return $this; | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
tests/Unit/Constraint/ValueProvider/Pseudo/ConstExpressionProviderTest.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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\ValueProvider\Pseudo; | ||
|
||
use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Pseudo\ConstExpressionProvider; | ||
use DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\ValueProvider\AbstractValueProviderTestCase; | ||
use DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\ValueProvider\Pseudo\data\ClassWithConsts; | ||
use phpDocumentor\Reflection\Fqsen; | ||
use phpDocumentor\Reflection\Types\Callable_; | ||
use phpDocumentor\Reflection\Types\Object_; | ||
use phpDocumentor\Reflection\Types\Self_; | ||
use ReflectionMethod; | ||
|
||
/** | ||
* @coversDefaultClass \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Pseudo\ConstExpressionProvider | ||
* @covers ::__construct | ||
*/ | ||
class ConstExpressionProviderTest extends AbstractValueProviderTestCase | ||
{ | ||
/** | ||
* @covers ::getValues | ||
*/ | ||
public function testGetValues(): void | ||
{ | ||
$valueProvider = new ConstExpressionProvider(new Object_(new Fqsen('\\' . ClassWithConsts::class)), 'CONST_*', null); | ||
$values = $valueProvider->getValues(); | ||
|
||
static::assertValueTypes($values, ['string']); | ||
static::assertContains('CONST_A', $values); | ||
static::assertContains('CONST_B', $values); | ||
static::assertNotContains('CONSTANT_A', $values); | ||
} | ||
|
||
/** | ||
* @covers ::getValues | ||
*/ | ||
public function testGetValuesSelf(): void | ||
{ | ||
$valueProvider = new ConstExpressionProvider(new Self_(), 'CONST_*', new ReflectionMethod(ClassWithConsts::class, 'setConst')); | ||
$values = $valueProvider->getValues(); | ||
|
||
static::assertValueTypes($values, ['string']); | ||
static::assertContains('CONST_A', $values); | ||
static::assertContains('CONST_B', $values); | ||
static::assertNotContains('CONSTANT_A', $values); | ||
} | ||
|
||
/** | ||
* @covers ::getValues | ||
*/ | ||
public function testGetValuesInvalidType(): void | ||
{ | ||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('ConstExpressionProvider can only be used with object or self typehints'); | ||
|
||
$valueProvider = new ConstExpressionProvider(new Callable_([]), 'CONST_*', null); | ||
$valueProvider->getValues(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Unit/Constraint/ValueProvider/Pseudo/data/ClassWithConsts.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\ValueProvider\Pseudo\data; | ||
|
||
class ClassWithConsts | ||
{ | ||
public const CONST_A = 'CONST_A'; | ||
public const CONST_B = 'CONST_B'; | ||
public const CONSTANT_A = 'CONSTANT_A'; | ||
|
||
/** @var string */ | ||
private string $const; | ||
|
||
/** | ||
* @param self::CONST_* $const | ||
*/ | ||
public function setConst(string $const): self | ||
{ | ||
$this->const = $const; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return self::CONST_* | ||
*/ | ||
public function getConst(): string | ||
{ | ||
return $this->const; | ||
} | ||
} |