Completed
Push — develop ( 9ed2dd...34c48b )
by Peter
18:31 queued 06:00
created

src/WebinoDraw/Dom/NodeTrait.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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) 2015-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\Dom;
12
13
use WebinoDraw\Exception;
14
15
/**
16
 * Class NodeTrait
17
 */
18
trait NodeTrait
19
{
20
    /**
21
     * @var callable[]
22
     */
23
    private $onReplace = [];
24
25
    /**
26
     * @param callable $callback
27
     * @return $this
28
     */
29
    public function setOnReplace(callable $callback)
30
    {
31
        $this->onReplace[] = $callback;
32
        return $this;
33
    }
34
35
    /**
36
     * @return Document|null
37
     */
38
    public function getOwnerDocument()
39
    {
40
        return isset($this->ownerDocument) ? $this->ownerDocument : null;
0 ignored issues
show
The property ownerDocument does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
    }
42
43
    /**
44
     * @return Element|null
45
     */
46
    public function getParentNode()
47
    {
48
        return isset($this->parentNode) ? $this->parentNode : null;
0 ignored issues
show
The property parentNode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
    }
50
51
    /**
52
     * @param string $html
53
     * @return NodeInterface
54
     * @throws Exception\LogicException
55
     */
56
    public function replaceWith($html)
57
    {
58
        $frag = $this->getOwnerDocument()->createDocumentFragment();
59
        $frag->appendXml($html);
60
61
        $parentNode = $this->getParentNode();
62
        $newNode    = $parentNode->insertBefore($frag, $this);
63
64
        if (!empty($this->onReplace)) {
65
            foreach ($this->onReplace as $onReplace) {
66
                call_user_func($onReplace, $newNode, $this, $frag);
67
            }
68
        }
69
70
        $parentNode->removeChild($this);
71
        return $newNode;
72
    }
73
}
74