Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHPStan extension for dynamic properties #49

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"autoload": {
"psr-4": {
"gapple\\StructuredFields\\": "src/",
"gapple\\StructuredFields\\PHPStan\\": "phpstan-extension/src/",
"gapple\\Tests\\StructuredFields\\": "tests/"
}
},
Expand Down
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);
}
}
87 changes: 87 additions & 0 deletions phpstan-extension/src/DynamicPropertyReflection.php
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();
}
}
9 changes: 5 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ parameters:
- src
- tests

universalObjectCratesClasses:
- gapple\StructuredFields\Dictionary
- gapple\StructuredFields\Parameters

featureToggles:
readOnlyByPhpDoc: true

ignoreErrors:
-
message: '#Unreachable statement - code above always terminates.#'
path: src/TupleTrait.php

services:
- class: gapple\StructuredFields\PHPStan\DynamicPropertiesClassReflectionExtension
tags:
- phpstan.broker.propertiesClassReflectionExtension