PHPStan\Rules\Classes\InstantiationRule::processNode PHP Method

processNode() public method

public processNode ( PhpParser\Node $node, Scope $scope ) : array
$node PhpParser\Node
$scope PHPStan\Analyser\Scope
return array
    public function processNode(Node $node, Scope $scope) : array
    {
        if (!$node->class instanceof \PhpParser\Node\Name) {
            return [];
        }
        $class = (string) $node->class;
        if ($class === 'static') {
            return [];
        }
        if ($class === 'self') {
            $class = $scope->getClass();
            if ($class === null) {
                return [];
            }
        }
        if (!$this->broker->hasClass($class)) {
            return [sprintf('Instantiated class %s not found.', $class)];
        }
        $classReflection = $this->broker->getClass($class);
        if ($classReflection->isInterface()) {
            return [sprintf('Cannot instantiate interface %s.', $classReflection->getName())];
        }
        if ($classReflection->isAbstract()) {
            return [sprintf('Instantiated class %s is abstract.', $classReflection->getName())];
        }
        if (!$classReflection->hasMethod('__construct') && !$classReflection->hasMethod($class)) {
            if (count($node->args) > 0) {
                return [sprintf('Class %s does not have a constructor and must be instantiated without any parameters.', $classReflection->getName())];
            }
            return [];
        }
        return $this->check->check($classReflection->hasMethod('__construct') ? $classReflection->getMethod('__construct') : $classReflection->getMethod($class), $node, ['Class ' . $classReflection->getName() . ' constructor invoked with %d parameter, %d required.', 'Class ' . $classReflection->getName() . ' constructor invoked with %d parameters, %d required.', 'Class ' . $classReflection->getName() . ' constructor invoked with %d parameter, at least %d required.', 'Class ' . $classReflection->getName() . ' constructor invoked with %d parameters, at least %d required.', 'Class ' . $classReflection->getName() . ' constructor invoked with %d parameter, %d-%d required.', 'Class ' . $classReflection->getName() . ' constructor invoked with %d parameters, %d-%d required.']);
    }