184813a657cf6cab39406fc02a1715f74ed0540a
[yaffs-website] / vendor / nikic / php-parser / lib / PhpParser / Builder / Param.php
1 <?php declare(strict_types=1);
2
3 namespace PhpParser\Builder;
4
5 use PhpParser;
6 use PhpParser\BuilderHelpers;
7 use PhpParser\Node;
8
9 class Param implements PhpParser\Builder
10 {
11     protected $name;
12
13     protected $default = null;
14
15     /** @var string|Node\Name|Node\NullableType|null */
16     protected $type = null;
17
18     protected $byRef = false;
19
20     protected $variadic = false;
21
22     /**
23      * Creates a parameter builder.
24      *
25      * @param string $name Name of the parameter
26      */
27     public function __construct(string $name) {
28         $this->name = $name;
29     }
30
31     /**
32      * Sets default value for the parameter.
33      *
34      * @param mixed $value Default value to use
35      *
36      * @return $this The builder instance (for fluid interface)
37      */
38     public function setDefault($value) {
39         $this->default = BuilderHelpers::normalizeValue($value);
40
41         return $this;
42     }
43
44     /**
45      * Sets type for the parameter.
46      *
47      * @param string|Node\Name|Node\NullableType $type Parameter type
48      *
49      * @return $this The builder instance (for fluid interface)
50      */
51     public function setType($type) {
52         $this->type = BuilderHelpers::normalizeType($type);
53         if ($this->type == 'void') {
54             throw new \LogicException('Parameter type cannot be void');
55         }
56
57         return $this;
58     }
59
60     /**
61      * Sets type for the parameter.
62      *
63      * @param string|Node\Name|Node\NullableType $type Parameter type
64      *
65      * @return $this The builder instance (for fluid interface)
66      *
67      * @deprecated Use setType() instead
68      */
69     public function setTypeHint($type) {
70         return $this->setType($type);
71     }
72
73     /**
74      * Make the parameter accept the value by reference.
75      *
76      * @return $this The builder instance (for fluid interface)
77      */
78     public function makeByRef() {
79         $this->byRef = true;
80
81         return $this;
82     }
83
84     /**
85      * Make the parameter variadic
86      *
87      * @return $this The builder instance (for fluid interface)
88      */
89     public function makeVariadic() {
90         $this->variadic = true;
91
92         return $this;
93     }
94
95     /**
96      * Returns the built parameter node.
97      *
98      * @return Node\Param The built parameter node
99      */
100     public function getNode() : Node {
101         return new Node\Param(
102             new Node\Expr\Variable($this->name),
103             $this->default, $this->type, $this->byRef, $this->variadic
104         );
105     }
106 }