Skip to content

Commit ac51c9e

Browse files
committed
added PHP 7.0 scalar and return type hints
1 parent 4ab52c0 commit ac51c9e

17 files changed

+124
-352
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ClassType
6666
* @param string|object
6767
* @return static
6868
*/
69-
public static function from($class)
69+
public static function from($class): self
7070
{
7171
if ($class instanceof \ReflectionClass) {
7272
trigger_error(__METHOD__ . '() accepts only class name or object.', E_USER_DEPRECATED);
@@ -77,20 +77,14 @@ public static function from($class)
7777
}
7878

7979

80-
/**
81-
* @param string|NULL
82-
*/
83-
public function __construct($name = NULL, PhpNamespace $namespace = NULL)
80+
public function __construct(string $name = NULL, PhpNamespace $namespace = NULL)
8481
{
8582
$this->setName($name);
8683
$this->namespace = $namespace;
8784
}
8885

8986

90-
/**
91-
* @return string PHP code
92-
*/
93-
public function __toString()
87+
public function __toString(): string
9488
{
9589
$traits = [];
9690
foreach ($this->traits as $trait => $resolutions) {
@@ -148,7 +142,7 @@ public function getNamespace()
148142
* @param string|NULL
149143
* @return static
150144
*/
151-
public function setName($name)
145+
public function setName($name): self
152146
{
153147
if ($name !== NULL && !Helpers::isIdentifier($name)) {
154148
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
@@ -168,10 +162,9 @@ public function getName()
168162

169163

170164
/**
171-
* @param string
172165
* @return static
173166
*/
174-
public function setType($type)
167+
public function setType(string $type): self
175168
{
176169
if (!in_array($type, ['class', 'interface', 'trait'], TRUE)) {
177170
throw new Nette\InvalidArgumentException('Argument must be class|interface|trait.');
@@ -181,50 +174,39 @@ public function setType($type)
181174
}
182175

183176

184-
/**
185-
* @return string
186-
*/
187-
public function getType()
177+
public function getType(): string
188178
{
189179
return $this->type;
190180
}
191181

192182

193183
/**
194-
* @param bool
195184
* @return static
196185
*/
197-
public function setFinal($state = TRUE)
186+
public function setFinal(bool $state = TRUE): self
198187
{
199188
$this->final = (bool) $state;
200189
return $this;
201190
}
202191

203192

204-
/**
205-
* @return bool
206-
*/
207-
public function isFinal()
193+
public function isFinal(): bool
208194
{
209195
return $this->final;
210196
}
211197

212198

213199
/**
214-
* @param bool
215200
* @return static
216201
*/
217-
public function setAbstract($state = TRUE)
202+
public function setAbstract(bool $state = TRUE): self
218203
{
219204
$this->abstract = (bool) $state;
220205
return $this;
221206
}
222207

223208

224-
/**
225-
* @return bool
226-
*/
227-
public function isAbstract()
209+
public function isAbstract(): bool
228210
{
229211
return $this->abstract;
230212
}
@@ -234,7 +216,7 @@ public function isAbstract()
234216
* @param string|string[]
235217
* @return static
236218
*/
237-
public function setExtends($types)
219+
public function setExtends($types): self
238220
{
239221
if (!is_string($types) && !(is_array($types) && Nette\Utils\Arrays::every($types, 'is_string'))) {
240222
throw new Nette\InvalidArgumentException('Argument must be string or string[].');
@@ -254,10 +236,9 @@ public function getExtends()
254236

255237

256238
/**
257-
* @param string
258239
* @return static
259240
*/
260-
public function addExtend($type)
241+
public function addExtend(string $type): self
261242
{
262243
$this->extends = (array) $this->extends;
263244
$this->extends[] = (string) $type;
@@ -269,7 +250,7 @@ public function addExtend($type)
269250
* @param string[]
270251
* @return static
271252
*/
272-
public function setImplements(array $types)
253+
public function setImplements(array $types): self
273254
{
274255
$this->implements = $types;
275256
return $this;
@@ -279,17 +260,16 @@ public function setImplements(array $types)
279260
/**
280261
* @return string[]
281262
*/
282-
public function getImplements()
263+
public function getImplements(): array
283264
{
284265
return $this->implements;
285266
}
286267

287268

288269
/**
289-
* @param string
290270
* @return static
291271
*/
292-
public function addImplement($type)
272+
public function addImplement(string $type): self
293273
{
294274
$this->implements[] = (string) $type;
295275
return $this;
@@ -300,7 +280,7 @@ public function addImplement($type)
300280
* @param string[]
301281
* @return static
302282
*/
303-
public function setTraits(array $traits)
283+
public function setTraits(array $traits): self
304284
{
305285
$this->traits = array_fill_keys($traits, []);
306286
return $this;
@@ -310,17 +290,16 @@ public function setTraits(array $traits)
310290
/**
311291
* @return string[]
312292
*/
313-
public function getTraits()
293+
public function getTraits(): array
314294
{
315295
return array_keys($this->traits);
316296
}
317297

318298

319299
/**
320-
* @param string
321300
* @return static
322301
*/
323-
public function addTrait($trait, array $resolutions = [])
302+
public function addTrait(string $trait, array $resolutions = []): self
324303
{
325304
$this->traits[$trait] = $resolutions;
326305
return $this;
@@ -331,7 +310,7 @@ public function addTrait($trait, array $resolutions = [])
331310
* @deprecated use setConstants()
332311
* @return static
333312
*/
334-
public function setConsts(array $consts)
313+
public function setConsts(array $consts): self
335314
{
336315
trigger_error(__METHOD__ . '() is deprecated, use setConstants()', E_USER_DEPRECATED);
337316
return $this->setConstants($consts);
@@ -340,9 +319,8 @@ public function setConsts(array $consts)
340319

341320
/**
342321
* @deprecated use getConstants()
343-
* @return array
344322
*/
345-
public function getConsts()
323+
public function getConsts(): array
346324
{
347325
trigger_error(__METHOD__ . '() is deprecated, use similar getConstants()', E_USER_DEPRECATED);
348326
return array_map(function ($const) { return $const->getValue(); }, $this->consts);
@@ -351,11 +329,9 @@ public function getConsts()
351329

352330
/**
353331
* @deprecated use addConstant()
354-
* @param string
355-
* @param mixed
356332
* @return static
357333
*/
358-
public function addConst($name, $value)
334+
public function addConst(string $name, $value): self
359335
{
360336
trigger_error(__METHOD__ . '() is deprecated, use similar addConstant()', E_USER_DEPRECATED);
361337
$this->addConstant($name, $value);
@@ -367,7 +343,7 @@ public function addConst($name, $value)
367343
* @param Constant[]|mixed[]
368344
* @return static
369345
*/
370-
public function setConstants(array $consts)
346+
public function setConstants(array $consts): self
371347
{
372348
$this->consts = [];
373349
foreach ($consts as $k => $v) {
@@ -381,18 +357,13 @@ public function setConstants(array $consts)
381357
/**
382358
* @return Constant[]
383359
*/
384-
public function getConstants()
360+
public function getConstants(): array
385361
{
386362
return $this->consts;
387363
}
388364

389365

390-
/**
391-
* @param string
392-
* @param mixed
393-
* @return Constant
394-
*/
395-
public function addConstant($name, $value)
366+
public function addConstant(string $name, $value): Constant
396367
{
397368
return $this->consts[$name] = (new Constant($name))->setValue($value);
398369
}
@@ -402,7 +373,7 @@ public function addConstant($name, $value)
402373
* @param Property[]
403374
* @return static
404375
*/
405-
public function setProperties(array $props)
376+
public function setProperties(array $props): self
406377
{
407378
$this->properties = [];
408379
foreach ($props as $v) {
@@ -418,16 +389,13 @@ public function setProperties(array $props)
418389
/**
419390
* @return Property[]
420391
*/
421-
public function getProperties()
392+
public function getProperties(): array
422393
{
423394
return $this->properties;
424395
}
425396

426397

427-
/**
428-
* @return Property
429-
*/
430-
public function getProperty($name)
398+
public function getProperty($name): Property
431399
{
432400
if (!isset($this->properties[$name])) {
433401
throw new Nette\InvalidArgumentException("Property '$name' not found.");
@@ -438,10 +406,8 @@ public function getProperty($name)
438406

439407
/**
440408
* @param string without $
441-
* @param mixed
442-
* @return Property
443409
*/
444-
public function addProperty($name, $value = NULL)
410+
public function addProperty(string $name, $value = NULL): Property
445411
{
446412
return $this->properties[$name] = (new Property($name))->setValue($value);
447413
}
@@ -451,7 +417,7 @@ public function addProperty($name, $value = NULL)
451417
* @param Method[]
452418
* @return static
453419
*/
454-
public function setMethods(array $methods)
420+
public function setMethods(array $methods): self
455421
{
456422
$this->methods = [];
457423
foreach ($methods as $v) {
@@ -467,16 +433,13 @@ public function setMethods(array $methods)
467433
/**
468434
* @return Method[]
469435
*/
470-
public function getMethods()
436+
public function getMethods(): array
471437
{
472438
return $this->methods;
473439
}
474440

475441

476-
/**
477-
* @return Method
478-
*/
479-
public function getMethod($name)
442+
public function getMethod($name): Method
480443
{
481444
if (!isset($this->methods[$name])) {
482445
throw new Nette\InvalidArgumentException("Method '$name' not found.");
@@ -485,11 +448,7 @@ public function getMethod($name)
485448
}
486449

487450

488-
/**
489-
* @param string
490-
* @return Method
491-
*/
492-
public function addMethod($name)
451+
public function addMethod(string $name): Method
493452
{
494453
$method = (new Method($name))->setNamespace($this->namespace);
495454
if ($this->type === 'interface') {

src/PhpGenerator/Closure.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ class Closure
2929
/**
3030
* @return static
3131
*/
32-
public static function from(\Closure $closure)
32+
public static function from(\Closure $closure): self
3333
{
3434
return (new Factory)->fromFunctionReflection(new \ReflectionFunction($closure));
3535
}
3636

3737

38-
/**
39-
* @return string PHP code
40-
*/
41-
public function __toString()
38+
public function __toString(): string
4239
{
4340
$uses = [];
4441
foreach ($this->uses as $param) {
@@ -57,26 +54,20 @@ public function __toString()
5754
* @param Parameter[]
5855
* @return static
5956
*/
60-
public function setUses(array $uses)
57+
public function setUses(array $uses): self
6158
{
6259
$this->uses = $uses;
6360
return $this;
6461
}
6562

6663

67-
/**
68-
* @return array
69-
*/
70-
public function getUses()
64+
public function getUses(): array
7165
{
7266
return $this->uses;
7367
}
7468

7569

76-
/**
77-
* @return Parameter
78-
*/
79-
public function addUse($name)
70+
public function addUse($name): Parameter
8071
{
8172
return $this->uses[] = new Parameter($name);
8273
}

src/PhpGenerator/Constant.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ class Constant
2929
/**
3030
* @return static
3131
*/
32-
public function setValue($val)
32+
public function setValue($val): self
3333
{
3434
$this->value = $val;
3535
return $this;
3636
}
3737

3838

39-
/**
40-
* @return mixed
41-
*/
4239
public function getValue()
4340
{
4441
return $this->value;

0 commit comments

Comments
 (0)