Skip to content

Commit

Permalink
Merge pull request #3 from TebexOllie/phpstan
Browse files Browse the repository at this point in the history
Add PHPStan compatibility
  • Loading branch information
liam-wiltshire committed Dec 19, 2023
2 parents ff4157b + 0ca69a4 commit def3aef
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"require-dev": {
"phpunit/phpunit": "^7.0.0",
"squizlabs/php_codesniffer" : "^3.0.0",
"phpunit/php-code-coverage": "^6.0.0"
"phpunit/php-code-coverage": "^6.0.0",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
-
class: LiamWiltshire\LaravelModelMeta\PHPStan\ModelAttributeExtension
tags:
- phpstan.broker.propertiesClassReflectionExtension
95 changes: 95 additions & 0 deletions src/PHPStan/MetaProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace LiamWiltshire\LaravelModelMeta\PHPStan;

use Illuminate\Database\Eloquent\Model;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

class MetaProperty implements PropertyReflection
{

private $classReflection;
private $propertyName;

public function __construct(ClassReflection $classReflection, string $propertyName)
{
$this->classReflection = $classReflection;
$this->propertyName = $propertyName;
}

public function isStatic(): bool
{
return false;
}

public function isPrivate(): bool
{
return false;
}

public function isPublic(): bool
{
return true;
}

public function isReadable(): bool
{
return true;
}

public function isWritable(): bool
{
return true;
}

public function isDeprecated(): TrinaryLogic
{
return false;
}

public function isInternal(): TrinaryLogic
{
return false;
}

public function canChangeTypeAfterAssignment(): bool
{
return true;
}

public function getReadableType(): Type
{
return new MixedType();
}

public function getWritableType(): Type
{
return new MixedType();
}

public function getDeclaringClass(): \PHPStan\Reflection\ClassReflection
{
return $this->classReflection;
}

public function getPropertyName(): string
{
return $this->propertyName;
}

public function getDocComment(): ?string
{
return null;
}

public function getDeprecatedDescription(): ?string
{
return null;
}
}
29 changes: 29 additions & 0 deletions src/PHPStan/ModelAttributeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace LiamWiltshire\LaravelModelMeta\PHPStan;

use Illuminate\Database\Eloquent\Model;
use LiamWiltshire\LaravelModelMeta\Concerns\HasMeta;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use Tebex\Checkout\Models\RecurringPayment;

class ModelAttributeExtension implements PropertiesClassReflectionExtension
{
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
foreach ($classReflection->getTraits() as $trait) {
if ($trait->getName() === (string)HasMeta::class) {
return true;
}
}

return false;
}

public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
return new MetaProperty($classReflection, $propertyName);
}
}

0 comments on commit def3aef

Please sign in to comment.