-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PHPStan extension for dynamic properties on Dictionary and Parame…
…ters
- Loading branch information
Showing
4 changed files
with
122 additions
and
4 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
29 changes: 29 additions & 0 deletions
29
phpstan-extension/src/DynamicPropertiesClassReflectionExtension.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,29 @@ | ||
<?php | ||
|
||
namespace gapple\StructuredFields\PHPStan; | ||
|
||
use gapple\StructuredFields\Dictionary; | ||
use gapple\StructuredFields\Parameters; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\PropertiesClassReflectionExtension; | ||
use PHPStan\Reflection\PropertyReflection; | ||
|
||
class DynamicPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension | ||
{ | ||
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool | ||
{ | ||
return ( | ||
$classReflection->is(Dictionary::class) | ||
|| $classReflection->is(Parameters::class) | ||
) | ||
&& preg_match('/^[a-z*][a-z0-9.*_-]*$/', $propertyName); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection | ||
{ | ||
return new DynamicPropertyReflection($classReflection); | ||
} | ||
} |
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,87 @@ | ||
<?php | ||
|
||
namespace gapple\StructuredFields\PHPStan; | ||
|
||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\Type; | ||
|
||
class DynamicPropertyReflection implements PropertyReflection | ||
{ | ||
/** | ||
* @var ClassReflection | ||
*/ | ||
private $classReflection; | ||
|
||
public function __construct(ClassReflection $classReflection) | ||
{ | ||
$this->classReflection = $classReflection; | ||
} | ||
|
||
public function getDeclaringClass(): \PHPStan\Reflection\ClassReflection | ||
{ | ||
return $this->classReflection; | ||
} | ||
|
||
public function isStatic(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isPrivate(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isPublic(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getDocComment(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function getReadableType(): Type | ||
{ | ||
return new MixedType(); | ||
} | ||
|
||
public function getWritableType(): Type | ||
{ | ||
return new MixedType(); | ||
} | ||
|
||
public function canChangeTypeAfterAssignment(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isReadable(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isWritable(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isDeprecated(): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createMaybe(); | ||
} | ||
|
||
public function getDeprecatedDescription(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function isInternal(): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createMaybe(); | ||
} | ||
} |
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