Project

General

Profile

Feature #96652 » TestingRule.php

Oliver Hader, 2022-01-26 11:47

 
<?php

declare(strict_types=1);

namespace TYPO3\CMS\Install\PhpStan;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\ObjectType;
use TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer;

class TestingRule implements Rule
{
public function getNodeType(): string
{
return Node\Expr\MethodCall::class;
}

/**
* @param Node\Expr\MethodCall $node
* @param Scope $scope
* @return array
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$node->var instanceof Node\Expr\Variable
|| !is_string($node->var->name)
|| !$node->name instanceof Node\Identifier
|| (string)$node->name !== 'render'
) {
return [];
}
// needs to be enhanced for other expressions, not just `$node->var->name`
$variable = (string)$node->var->name;
$variableType = $scope->getVariableType($variable);
$calledClass = null;
$calledMethod = (string)$node->name;
if ($variableType instanceof ObjectType) {
$calledClass = $variableType->getClassName();
} else {
var_dump(get_class($variableType));
}
if ($calledClass === VimeoRenderer::class && count($node->args) > 4) {
// RuleErrorBuilder does not seem to work in PhpStan 0.12.whatever
return [sprintf(
'Avoid using more than 4 args in %s->%s, got %d',
$calledClass,
$calledMethod,
count($node->args)
)];
}
return [];
}
}
(4-4/4)