These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of Railt package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | */ |
||
8 | declare(strict_types=1); |
||
9 | |||
10 | namespace Railt\Reflection\Common; |
||
11 | |||
12 | use Railt\Io\Readable; |
||
13 | use Railt\Reflection\Contracts\Definition; |
||
14 | use Railt\Reflection\Contracts\Document; |
||
15 | |||
16 | /** |
||
17 | * Trait Jsonable |
||
18 | */ |
||
19 | trait Jsonable |
||
20 | { |
||
21 | /** |
||
22 | * @return array |
||
23 | */ |
||
24 | public function jsonSerialize(): array |
||
25 | { |
||
26 | $result = []; |
||
27 | |||
28 | foreach ($this->getObjectFields() as $field => $value) { |
||
0 ignored issues
–
show
|
|||
29 | foreach ($this->jsonNonRenderableTypes() as $type) { |
||
30 | if ($value instanceof $type) { |
||
31 | continue 2; |
||
32 | } |
||
33 | } |
||
34 | |||
35 | foreach ($this->jsonNonRenderableFields() as $name) { |
||
36 | if ($field === $name) { |
||
37 | continue 2; |
||
38 | } |
||
39 | } |
||
40 | |||
41 | $result[$field] = $value; |
||
42 | } |
||
43 | |||
44 | // Additional information about type |
||
45 | if ($this instanceof Definition) { |
||
46 | $result['__typename'] = $this::getType()->getName(); |
||
47 | } |
||
48 | |||
49 | return $result; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return iterable|string[] |
||
54 | */ |
||
55 | private function jsonNonRenderableTypes(): iterable |
||
56 | { |
||
57 | yield Readable::class; |
||
58 | yield Document::class; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return iterable|string[] |
||
63 | */ |
||
64 | private function jsonNonRenderableFields(): iterable |
||
65 | { |
||
66 | // Recursive relation exclusion |
||
67 | yield 'parent'; |
||
68 | |||
69 | // Dictionary relation exclusion |
||
70 | yield 'dictionary'; |
||
71 | |||
72 | // File information exclusion |
||
73 | yield 'offset'; |
||
74 | //yield 'line'; |
||
75 | yield 'column'; |
||
76 | |||
77 | // Reverse superfluous inheritance information |
||
78 | yield 'extendedBy'; |
||
79 | } |
||
80 | } |
||
81 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.