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

Breadcrumb

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

class MethodNode

Method node.

@author Konstantin Kudryashov <ever.zet@gmail.com>

Hierarchy

  • class \Prophecy\Doubler\Generator\Node\MethodNode

Expanded class hierarchy of MethodNode

5 files declare their use of MethodNode
DisableConstructorPatch.php in vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/DisableConstructorPatch.php
MagicCallPatch.php in vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/MagicCallPatch.php
ProphecySubjectPatch.php in vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/ProphecySubjectPatch.php
SplFileInfoPatch.php in vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/SplFileInfoPatch.php
TraversablePatch.php in vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/TraversablePatch.php

File

vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/Node/MethodNode.php, line 21

Namespace

Prophecy\Doubler\Generator\Node
View source
class MethodNode {
    private $name;
    private $code;
    
    /**
     * @var string
     *
     * @phpstan-var 'public'|'private'|'protected'
     */
    private $visibility = 'public';
    
    /**
     * @var bool
     */
    private $static = false;
    
    /**
     * @var bool
     */
    private $returnsReference = false;
    
    /** @var ReturnTypeNode */
    private $returnTypeNode;
    
    /**
     * @var list<ArgumentNode>
     */
    private $arguments = array();
    // Used to accept an optional third argument with the deprecated Prophecy\Doubler\Generator\TypeHintReference so careful when adding a new argument in a minor version.
    
    /**
     * @param string      $name
     * @param string|null $code
     */
    public function __construct($name, $code = null) {
        $this->name = $name;
        $this->code = $code;
        $this->returnTypeNode = new ReturnTypeNode();
    }
    
    /**
     * @return string
     *
     * @phpstan-return 'public'|'private'|'protected'
     */
    public function getVisibility() {
        return $this->visibility;
    }
    
    /**
     * @param string $visibility
     *
     * @return void
     */
    public function setVisibility($visibility) {
        $visibility = strtolower($visibility);
        if (!\in_array($visibility, array(
            'public',
            'private',
            'protected',
        ), true)) {
            throw new InvalidArgumentException(sprintf('`%s` method visibility is not supported.', $visibility));
        }
        $this->visibility = $visibility;
    }
    
    /**
     * @return bool
     */
    public function isStatic() {
        return $this->static;
    }
    
    /**
     * @param bool $static
     *
     * @return void
     */
    public function setStatic($static = true) {
        $this->static = (bool) $static;
    }
    
    /**
     * @return bool
     */
    public function returnsReference() {
        return $this->returnsReference;
    }
    
    /**
     * @return void
     */
    public function setReturnsReference() {
        $this->returnsReference = true;
    }
    
    /**
     * @return string
     */
    public function getName() {
        return $this->name;
    }
    
    /**
     * @return void
     */
    public function addArgument(ArgumentNode $argument) {
        $this->arguments[] = $argument;
    }
    
    /**
     * @return list<ArgumentNode>
     */
    public function getArguments() {
        return $this->arguments;
    }
    
    /**
     * @deprecated use getReturnTypeNode instead
     * @return bool
     */
    public function hasReturnType() {
        return (bool) $this->returnTypeNode
            ->getNonNullTypes();
    }
    public function setReturnTypeNode(ReturnTypeNode $returnTypeNode) : void {
        $this->returnTypeNode = $returnTypeNode;
    }
    
    /**
     * @deprecated use setReturnTypeNode instead
     * @param string $type
     *
     * @return void
     */
    public function setReturnType($type = null) {
        $this->returnTypeNode = $type === '' || $type === null ? new ReturnTypeNode() : new ReturnTypeNode($type);
    }
    
    /**
     * @deprecated use setReturnTypeNode instead
     * @param bool $bool
     *
     * @return void
     */
    public function setNullableReturnType($bool = true) {
        if ($bool) {
            $this->returnTypeNode = new ReturnTypeNode('null', ...$this->returnTypeNode
                ->getTypes());
        }
        else {
            $this->returnTypeNode = new ReturnTypeNode(...$this->returnTypeNode
                ->getNonNullTypes());
        }
    }
    
    /**
     * @deprecated use getReturnTypeNode instead
     * @return string|null
     */
    public function getReturnType() {
        if ($types = $this->returnTypeNode
            ->getNonNullTypes()) {
            return $types[0];
        }
        return null;
    }
    public function getReturnTypeNode() : ReturnTypeNode {
        return $this->returnTypeNode;
    }
    
    /**
     * @deprecated use getReturnTypeNode instead
     * @return bool
     */
    public function hasNullableReturnType() {
        return $this->returnTypeNode
            ->canUseNullShorthand();
    }
    
    /**
     * @param string $code
     *
     * @return void
     */
    public function setCode($code) {
        $this->code = $code;
    }
    
    /**
     * @return string
     */
    public function getCode() {
        if ($this->returnsReference) {
            return "throw new \\Prophecy\\Exception\\Doubler\\ReturnByReferenceException('Returning by reference not supported', get_class(\$this), '{$this->name}');";
        }
        return (string) $this->code;
    }
    
    /**
     * @return void
     */
    public function useParentCode() {
        $this->code = sprintf('return parent::%s(%s);', $this->getName(), implode(', ', array_map(array(
            $this,
            'generateArgument',
        ), $this->arguments)));
    }
    
    /**
     * @return string
     */
    private function generateArgument(ArgumentNode $arg) {
        $argument = '$' . $arg->getName();
        if ($arg->isVariadic()) {
            $argument = '...' . $argument;
        }
        return $argument;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary
MethodNode::$arguments private property
MethodNode::$code private property
MethodNode::$name private property
MethodNode::$returnsReference private property
MethodNode::$returnTypeNode private property @var ReturnTypeNode
MethodNode::$static private property
MethodNode::$visibility private property @phpstan-var &#039;public&#039;|&#039;private&#039;|&#039;protected&#039;
MethodNode::addArgument public function
MethodNode::generateArgument private function
MethodNode::getArguments public function
MethodNode::getCode public function
MethodNode::getName public function
MethodNode::getReturnType Deprecated public function
MethodNode::getReturnTypeNode public function
MethodNode::getVisibility public function @phpstan-return &#039;public&#039;|&#039;private&#039;|&#039;protected&#039;
MethodNode::hasNullableReturnType Deprecated public function
MethodNode::hasReturnType Deprecated public function
MethodNode::isStatic public function
MethodNode::returnsReference public function
MethodNode::setCode public function
MethodNode::setNullableReturnType Deprecated public function
MethodNode::setReturnsReference public function
MethodNode::setReturnType Deprecated public function
MethodNode::setReturnTypeNode public function
MethodNode::setStatic public function
MethodNode::setVisibility public function
MethodNode::useParentCode public function
MethodNode::__construct public function
RSS feed
Powered by Drupal