Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/Reflection/Nette/HtmlClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use function in_array;

class HtmlClassReflectionExtension implements MethodsClassReflectionExtension, PropertiesClassReflectionExtension
{

public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
return $classReflection->is('Nette\Utils\Html');
// The static factories declared in the stub, which would otherwise be taken for attribute setters
return $classReflection->is('Nette\Utils\Html') && !in_array($methodName, ['text', 'html'], true);
}

public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
Expand Down
2 changes: 2 additions & 0 deletions stubs/Utils/Html.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Nette\Utils;
/**
* @implements \IteratorAggregate<int, self|string>
* @implements \ArrayAccess<int, self|string>
* @method static static text(mixed $text)
* @method static static html(mixed $html)
Comment on lines +8 to +9
*/
class Html implements \ArrayAccess, \IteratorAggregate
{
Expand Down
16 changes: 14 additions & 2 deletions tests/Reflection/Nette/HtmlClassReflectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,22 @@ public function dataHasMethod(): array
return [
[
Html::class,
'href',
true,
],
[
Html::class,
'text',
false,
],
[
Html::class,
'html',
false,
],
[
stdClass::class,
'href',
false,
],
];
Expand All @@ -40,10 +52,10 @@ public function dataHasMethod(): array
/**
* @dataProvider dataHasMethod
*/
public function testHasMethod(string $className, bool $result): void
public function testHasMethod(string $className, string $methodName, bool $result): void
{
$classReflection = $this->reflectionProvider->getClass($className);
self::assertSame($result, $this->extension->hasMethod($classReflection, 'href'));
self::assertSame($result, $this->extension->hasMethod($classReflection, $methodName));
}

public function testGetMethod(): void
Expand Down
32 changes: 32 additions & 0 deletions tests/Rule/Nette/HtmlStaticFactoriesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rule\Nette;

use PHPStan\Rules\Methods\CallStaticMethodsRule;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<CallStaticMethodsRule>
*/
class HtmlStaticFactoriesTest extends RuleTestCase
{

protected function getRule(): Rule
{
return self::getContainer()->getByType(CallStaticMethodsRule::class); // @phpstan-ignore phpstanApi.classConstant
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/html-static-factories.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../../extension.neon',
];
}

}
12 changes: 12 additions & 0 deletions tests/Rule/Nette/data/html-static-factories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace HtmlStaticFactories;

use Nette\Utils\Html;

function factories(): Html
{
$text = Html::text('text');
$html = Html::html('<b>html</b>');
return $text->addHtml($html);
}
Loading