These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types = 1); |
||
2 | |||
3 | namespace DaveRandom\CallbackValidator; |
||
4 | |||
5 | final class ReturnType extends Type |
||
6 | { |
||
7 | /** |
||
8 | * Contravariant return types allow implementors to specify a supertype of that which is specified in the prototype |
||
9 | * Usually this isn't a good idea, it's not type-safe, do not use unless you understand what you are doing! |
||
10 | */ |
||
11 | const CONTRAVARIANT = 0x01 << 16; |
||
12 | |||
13 | /** |
||
14 | * Covariant return types allow implementors to specify a subtype of that which is specified in the prototype |
||
15 | */ |
||
16 | const COVARIANT = 0x02 << 16; |
||
17 | |||
18 | /** |
||
19 | * @param \ReflectionFunctionAbstract $reflection |
||
20 | * @param int $flags |
||
21 | * @return ReturnType |
||
22 | */ |
||
23 | public static function createFromReflectionFunctionAbstract($reflection, $flags = 0) |
||
24 | { |
||
25 | if ($reflection->returnsReference()) { |
||
26 | $flags |= self::REFERENCE; |
||
27 | } |
||
28 | |||
29 | $typeName = null; |
||
30 | $typeReflection = $reflection->getReturnType(); |
||
31 | |||
32 | View Code Duplication | if ($typeReflection !== null) { |
|
1 ignored issue
–
show
|
|||
33 | $typeName = (string)$typeReflection; |
||
34 | |||
35 | if ($typeReflection->allowsNull()) { |
||
36 | $flags |= self::NULLABLE; |
||
37 | } |
||
38 | } |
||
39 | |||
40 | return new self($typeName, $flags); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param string|null $typeName |
||
45 | * @param int $flags |
||
46 | */ |
||
47 | public function __construct($typeName = null, $flags = self::COVARIANT) |
||
48 | { |
||
49 | $flags = (int)$flags; |
||
1 ignored issue
–
show
|
|||
50 | |||
51 | parent::__construct($typeName, $flags, $flags & self::COVARIANT, $flags & self::CONTRAVARIANT); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function __toString() |
||
58 | { |
||
59 | return $this->isNullable && $this->typeName !== null |
||
60 | ? '?' . $this->typeName |
||
61 | : (string)$this->typeName; |
||
62 | } |
||
63 | } |
||
64 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.