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

Breadcrumb

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

class MagicCallPatch

Discover Magical API using "@method" PHPDoc format.

@author Thomas Tourlourat <thomas@tourlourat.com> @author Kévin Dunglas <dunglas@gmail.com> @author Théo FIDRY <theo.fidry@gmail.com>

Hierarchy

  • class \Prophecy\Doubler\ClassPatch\MagicCallPatch implements \Prophecy\Doubler\ClassPatch\ClassPatchInterface

Expanded class hierarchy of MagicCallPatch

File

vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/MagicCallPatch.php, line 27

Namespace

Prophecy\Doubler\ClassPatch
View source
class MagicCallPatch implements ClassPatchInterface {
    const MAGIC_METHODS_WITH_ARGUMENTS = [
        '__call',
        '__callStatic',
        '__get',
        '__isset',
        '__set',
        '__set_state',
        '__unserialize',
        '__unset',
    ];
    private $tagRetriever;
    public function __construct(?MethodTagRetrieverInterface $tagRetriever = null) {
        $this->tagRetriever = null === $tagRetriever ? new ClassAndInterfaceTagRetriever() : $tagRetriever;
    }
    
    /**
     * Support any class
     *
     * @param ClassNode $node
     *
     * @return boolean
     */
    public function supports(ClassNode $node) {
        return true;
    }
    
    /**
     * Discover Magical API
     *
     * @param ClassNode $node
     */
    public function apply(ClassNode $node) {
        $types = array_filter($node->getInterfaces(), function ($interface) {
            return 0 !== strpos($interface, 'Prophecy\\');
        });
        $types[] = $node->getParentClass();
        foreach ($types as $type) {
            $reflectionClass = new \ReflectionClass($type);
            while ($reflectionClass) {
                $tagList = $this->tagRetriever
                    ->getTagList($reflectionClass);
                foreach ($tagList as $tag) {
                    $methodName = $tag->getMethodName();
                    if (empty($methodName)) {
                        continue;
                    }
                    if (!$reflectionClass->hasMethod($methodName)) {
                        $methodNode = new MethodNode($methodName);
                        // only magic methods can have a contract that needs to be enforced
                        if (in_array($methodName, self::MAGIC_METHODS_WITH_ARGUMENTS)) {
                            if (method_exists($tag, 'getParameters')) {
                                // Reflection Docblock 5.4.0+.
                                foreach ($tag->getParameters() as $argument) {
                                    $argumentNode = new ArgumentNode($argument->getName());
                                    $methodNode->addArgument($argumentNode);
                                }
                            }
                            else {
                                // Reflection Docblock < 5.4.0.
                                foreach ($tag->getArguments() as $argument) {
                                    $argumentNode = new ArgumentNode($argument['name']);
                                    $methodNode->addArgument($argumentNode);
                                }
                            }
                        }
                        $methodNode->setStatic($tag->isStatic());
                        $node->addMethod($methodNode);
                    }
                }
                $reflectionClass = $reflectionClass->getParentClass();
            }
        }
    }
    
    /**
     * Returns patch priority, which determines when patch will be applied.
     *
     * @return integer Priority number (higher - earlier)
     */
    public function getPriority() {
        return 50;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
MagicCallPatch::$tagRetriever private property
MagicCallPatch::apply public function Discover Magical API Overrides ClassPatchInterface::apply
MagicCallPatch::getPriority public function Returns patch priority, which determines when patch will be applied. Overrides ClassPatchInterface::getPriority
MagicCallPatch::MAGIC_METHODS_WITH_ARGUMENTS constant
MagicCallPatch::supports public function Support any class Overrides ClassPatchInterface::supports
MagicCallPatch::__construct public function

API Navigation

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