These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Serialize a DOM to RabbitFish Json: http://www.bramstein.com/projects/xsltjson/ |
||
4 | * |
||
5 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
||
6 | * @copyright Copyright (c) 2009-2017 FluentDOM Contributors |
||
7 | */ |
||
8 | |||
9 | namespace FluentDOM\Serializer\Json { |
||
10 | |||
11 | use FluentDOM\DOM\Xpath; |
||
12 | |||
13 | /** |
||
14 | * Serialize a DOM to RabbitFish Json: http://www.bramstein.com/projects/xsltjson/ |
||
15 | * |
||
16 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
||
17 | * @copyright Copyright (c) 2009-2017 FluentDOM Contributors |
||
18 | */ |
||
19 | class RabbitFish extends BadgerFish { |
||
20 | |||
21 | /** |
||
22 | * @param \DOMElement $node |
||
23 | * @return mixed |
||
24 | */ |
||
25 | 9 | protected function getNodes(\DOMElement $node) { |
|
26 | 9 | $xpath = new Xpath($node->ownerDocument); |
|
27 | 9 | $hasText = $xpath->evaluate('count(text()[normalize-space(.) != ""]) > 0', $node); |
|
28 | 9 | $hasElements = $xpath->evaluate('count(*) > 0', $node); |
|
29 | 9 | $attributes = new \stdClass(); |
|
30 | 9 | $this->addAttributes($attributes, $node, $xpath); |
|
31 | 9 | $this->addNamespaces($attributes, $node, $xpath); |
|
32 | 9 | $attributes = (array)$attributes; |
|
33 | 9 | if ($hasText && $hasElements) { |
|
34 | 2 | return $this->getNodesArray($node, $attributes, $xpath); |
|
35 | } |
||
36 | 9 | if ($hasText && count($attributes) === 0) { |
|
37 | 5 | return $node->nodeValue; |
|
1 ignored issue
–
show
|
|||
38 | } |
||
39 | 6 | return parent::getNodes($node); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param \DOMElement $node |
||
44 | * @param \stdClass|array $attributes |
||
45 | * @param Xpath $xpath |
||
46 | * @return array |
||
47 | */ |
||
48 | 2 | private function getNodesArray(\DOMElement $node, $attributes, $xpath): array { |
|
49 | 2 | $result = []; |
|
50 | 2 | foreach ($attributes as $name => $value) { |
|
51 | 1 | $child = new \stdClass(); |
|
52 | 1 | $child->{$name} = $value; |
|
53 | 1 | $result[] = $child; |
|
54 | } |
||
55 | 2 | foreach ($xpath->evaluate('*|text()[normalize-space(.) != ""]', $node) as $childNode) { |
|
56 | /** @var \DOMElement|\DOMText|\DOMCdataSection $childNode */ |
||
57 | 2 | if ($childNode instanceof \DOMElement) { |
|
58 | 2 | $child = new \stdClass(); |
|
59 | 2 | $child->{$childNode->nodeName} = $this->getNodes($childNode); |
|
60 | 2 | $result[] = $child; |
|
61 | 2 | } elseif (!$childNode->isWhitespaceInElementContent()) { |
|
62 | 2 | $result[] = $childNode->nodeValue; |
|
63 | } |
||
64 | } |
||
65 | 2 | return $result; |
|
66 | } |
||
67 | } |
||
68 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.