• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / php-generator / 21808450793

09 Feb 2026 12:39AM UTC coverage: 93.947% (+0.1%) from 93.811%
21808450793

push

github

dg
added CLAUDE.md

1816 of 1933 relevant lines covered (93.95%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.48
/src/PhpGenerator/Traits/FunctionLike.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\PhpGenerator\Traits;
11

12
use JetBrains\PhpStorm\Language;
13
use Nette;
14
use Nette\PhpGenerator\Dumper;
15
use Nette\PhpGenerator\Parameter;
16
use Nette\Utils\Type;
17
use function func_num_args;
18

19

20
/**
21
 * @internal
22
 */
23
trait FunctionLike
24
{
25
        private string $body = '';
26

27
        /** @var array<string, Parameter> */
28
        private array $parameters = [];
29
        private bool $variadic = false;
30
        private ?string $returnType = null;
31
        private bool $returnReference = false;
32
        private bool $returnNullable = false;
33

34

35
        /** @param  ?mixed[]  $args */
36
        public function setBody(
1✔
37
                #[Language('PHP')]
38
                string $code,
39
                ?array $args = null,
40
        ): static
41
        {
42
                $this->body = $args === null
1✔
43
                        ? $code
1✔
44
                        : (new Dumper)->format($code, ...$args);
1✔
45
                return $this;
1✔
46
        }
47

48

49
        public function getBody(): string
50
        {
51
                return $this->body;
1✔
52
        }
53

54

55
        /** @param  ?mixed[]  $args */
56
        public function addBody(
1✔
57
                #[Language('PHP')]
58
                string $code,
59
                ?array $args = null,
60
        ): static
61
        {
62
                $this->body .= ($args === null ? $code : (new Dumper)->format($code, ...$args)) . "\n";
1✔
63
                return $this;
1✔
64
        }
65

66

67
        /** @param list<Parameter>  $val */
68
        public function setParameters(array $val): static
1✔
69
        {
70
                (function (Parameter ...$val) {})(...$val);
1✔
71
                $this->parameters = [];
1✔
72
                foreach ($val as $v) {
1✔
73
                        $this->parameters[$v->getName()] = $v;
1✔
74
                }
75

76
                return $this;
1✔
77
        }
78

79

80
        /** @return array<string, Parameter> */
81
        public function getParameters(): array
82
        {
83
                return $this->parameters;
1✔
84
        }
85

86

87
        public function getParameter(string $name): Parameter
1✔
88
        {
89
                return $this->parameters[$name] ?? throw new Nette\InvalidArgumentException("Parameter '$name' not found.");
1✔
90
        }
91

92

93
        /**
94
         * Adds a parameter. If it already exists, it overwrites it.
95
         * @param  string  $name without $
96
         */
97
        public function addParameter(string $name, mixed $defaultValue = null): Parameter
1✔
98
        {
99
                $param = new Parameter($name);
1✔
100
                if (func_num_args() > 1) {
1✔
101
                        $param->setDefaultValue($defaultValue);
1✔
102
                }
103

104
                return $this->parameters[$name] = $param;
1✔
105
        }
106

107

108
        /**
109
         * @param  string  $name without $
110
         */
111
        public function removeParameter(string $name): static
1✔
112
        {
113
                unset($this->parameters[$name]);
1✔
114
                return $this;
1✔
115
        }
116

117

118
        public function hasParameter(string $name): bool
1✔
119
        {
120
                return isset($this->parameters[$name]);
1✔
121
        }
122

123

124
        public function setVariadic(bool $state = true): static
1✔
125
        {
126
                $this->variadic = $state;
1✔
127
                return $this;
1✔
128
        }
129

130

131
        public function isVariadic(): bool
132
        {
133
                return $this->variadic;
1✔
134
        }
135

136

137
        public function setReturnType(?string $type): static
1✔
138
        {
139
                $this->returnType = Nette\PhpGenerator\Helpers::validateType($type, $this->returnNullable);
1✔
140
                return $this;
1✔
141
        }
142

143

144
        /** @return ($asObject is true ? ?Type : ?string) */
145
        public function getReturnType(bool $asObject = false): Type|string|null
1✔
146
        {
147
                return $asObject && $this->returnType
1✔
148
                        ? Type::fromString($this->returnType)
×
149
                        : $this->returnType;
1✔
150
        }
151

152

153
        public function setReturnReference(bool $state = true): static
1✔
154
        {
155
                $this->returnReference = $state;
1✔
156
                return $this;
1✔
157
        }
158

159

160
        public function getReturnReference(): bool
161
        {
162
                return $this->returnReference;
1✔
163
        }
164

165

166
        public function setReturnNullable(bool $state = true): static
167
        {
168
                $this->returnNullable = $state;
×
169
                return $this;
×
170
        }
171

172

173
        public function isReturnNullable(): bool
174
        {
175
                return $this->returnNullable;
1✔
176
        }
177
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc