DrawAjaxJsonStrategy::createFragments()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\View\Strategy;
12
13
use WebinoDraw\Dom\Document;
14
use WebinoDraw\Event\AjaxEvent;
15
use WebinoDraw\Exception\RuntimeException;
16
17
/**
18
 * Draw matched containers and return the JSON XHTML of matched fragments
19
 */
20
class DrawAjaxJsonStrategy extends AbstractDrawAjaxStrategy
21
{
22
    /**
23
     * Return XHTML parts of nodes matched by XPath
24
     *
25
     * @param Document $dom
26
     * @param string $xpath
27
     * @return array
28
     * @throws Exception\RuntimeException
29
     */
30
    public function createFragments(Document $dom, $xpath)
31
    {
32
        $data = [];
33
        foreach ($dom->getXpath()->query($xpath) as $item) {
34
35
            $itemId = $item->getAttribute('id');
36
            if (empty($itemId)) {
37
                throw new RuntimeException('Required ajax fragment element id');
38
            }
39
40
            $data['fragment']['#' . $itemId] = $dom->saveHTML($item);
41
        }
42
43
        return $data;
44
    }
45
46
    /**
47
     * @param Document $dom
48
     * @param string $xpath
49
     * @return \WebinoDraw\Ajax\Json
50
     */
51
    protected function respond(Document $dom, $xpath)
52
    {
53
        $ajaxEvent = $this->getEvent();
54
        $ajaxEvent->setFragmentXpath($xpath);
55
56
        $this->getEventManager()->trigger(AjaxEvent::EVENT_AJAX, $ajaxEvent);
57
58
        return $ajaxEvent->getJson()
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $ajaxEvent->getJs...h()))->jsonSerialize(); (string) is incompatible with the return type declared by the abstract method WebinoDraw\View\Strategy...awAjaxStrategy::respond of type WebinoDraw\Ajax\Json.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
            ->merge($this->createFragments($dom, $ajaxEvent->getFragmentXpath()))
60
            ->jsonSerialize();
61
    }
62
}
63