Skip to content
Closed
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
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ parameters:
- forward

services:
-
class: PHPStan\Reflection\Nette\FormEventsReflectionExtension
tags:
- phpstan.broker.propertiesClassReflectionExtension

-
class: PHPStan\Reflection\Nette\HtmlClassReflectionExtension
tags:
Expand Down
107 changes: 107 additions & 0 deletions src/Reflection/Nette/FormEventsReflectionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php declare(strict_types=1);

namespace PHPStan\Reflection\Nette;

use Nette\Application\UI\Form;
use Nette\Utils\ArrayHash;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\CallableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VoidType;

final class FormEventsReflectionExtension implements PropertiesClassReflectionExtension
{

/**
* @var array<string, PropertyReflection>
*/
private $eventTypes;

private function initialize(ClassReflection $classReflection): bool
{
$this->eventTypes = [
'onSuccess' => self::createReflection(new CallableType([new NativeParameterReflection('form', false, new ObjectType(Form::class), PassedByReference::createNo(), false), new NativeParameterReflection('values', true, TypeCombinator::union(new ObjectType(ArrayHash::class), new ArrayType(new StringType(), new MixedType())), PassedByReference::createNo(), false)], new VoidType(), false), $classReflection),
'onError' => self::createReflection(new CallableType([new NativeParameterReflection('form', false, new ObjectType(Form::class), PassedByReference::createNo(), false)], new VoidType(), false), $classReflection),
'onSubmit' => self::createReflection(new CallableType([new NativeParameterReflection('form', false, new ObjectType(Form::class), PassedByReference::createNo(), false), new NativeParameterReflection('values', true, TypeCombinator::union(new ObjectType(ArrayHash::class), new ArrayType(new StringType(), new MixedType())), PassedByReference::createNo(), false)], new VoidType(), false), $classReflection),
'onRender' => self::createReflection(new CallableType([new NativeParameterReflection('form', false, new ObjectType(Form::class), PassedByReference::createNo(), false)], new VoidType(), false), $classReflection),
'onAttached' => self::createReflection(new CallableType([new NativeParameterReflection('form', false, new ObjectType(Form::class), PassedByReference::createNo(), false)], new VoidType(), false), $classReflection),
];

return true;
}

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
return ($classReflection->isSubclassOf(Form::class) || $classReflection->getName() === Form::class)
&& $this->initialize($classReflection)
&& array_key_exists($propertyName, $this->eventTypes);
}

public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
return $this->eventTypes[$propertyName];
}

private static function createReflection(Type $propertyType, ClassReflection $classReflection): PropertyReflection
{
return new class($propertyType, $classReflection) implements PropertyReflection
{
/** @var Type */
private $propertyType;

/** @var ClassReflection */
private $classReflection;

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

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

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

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

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

public function getType(): Type
{
return $this->propertyType;
}

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

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

}
37 changes: 37 additions & 0 deletions tests/Reflection/Nette/FormEventsReflectionExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\Nette;

use PHPStan\Type\VerbosityLevel;

final class FormEventsReflectionExtensionTest extends \PHPStan\Testing\TestCase
{

/** @var \PHPStan\Broker\Broker */
private $broker;

/** @var \PHPStan\Reflection\Nette\FormEventsReflectionExtension */
private $extension;

protected function setUp(): void
{
$this->broker = $this->createBroker();
$this->extension = new FormEventsReflectionExtension();
}

public function testGetProperty(): void
{
$classReflection = $this->broker->getClass(\Nette\Application\UI\Form::class);
self::assertTrue($this->extension->hasProperty($classReflection, 'onSuccess'));
$propertyReflection = $this->extension->getProperty($classReflection, 'onSuccess');
self::assertSame($classReflection, $propertyReflection->getDeclaringClass());
self::assertFalse($propertyReflection->isStatic());
self::assertFalse($propertyReflection->isPrivate());
self::assertTrue($propertyReflection->isPublic());
self::assertSame(
sprintf('callable(%s, array<string, mixed>|%s): void', \Nette\Application\UI\Form::class, \Nette\Utils\ArrayHash::class),
$propertyReflection->getType()->describe(VerbosityLevel::value())
);
}

}