Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DisableConstructorPatch.php

function DisableConstructorPatch::apply

Makes all class constructor arguments optional.

Parameters

ClassNode $node:

Overrides ClassPatchInterface::apply

File

vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/DisableConstructorPatch.php, line 43

Class

DisableConstructorPatch
Disable constructor. Makes all constructor arguments optional.

Namespace

Prophecy\Doubler\ClassPatch

Code

public function apply(ClassNode $node) {
    if (!$node->isExtendable('__construct')) {
        return;
    }
    if (!$node->hasMethod('__construct')) {
        $node->addMethod(new MethodNode('__construct', ''));
        return;
    }
    $constructor = $node->getMethod('__construct');
    \assert($constructor !== null);
    foreach ($constructor->getArguments() as $argument) {
        $argument->setDefault(null);
        $types = $argument->getTypeNode()
            ->getNonNullTypes();
        if ([] === $types || [
            'mixed',
        ] === $types || [
            '\\mixed',
        ] === $types) {
            continue;
        }
        $argument->setTypeNode(new ArgumentTypeNode('null', ...$types));
    }
    $constructor->setCode(<<<PHP
if (0 < func_num_args()) {
    call_user_func_array(array(parent::class, '__construct'), func_get_args());
}
PHP
);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal