WhiteSpaceRemovalSubscriber   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 71
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
A performXpathQuery() 0 8 2
B removeWhitespace() 0 27 10
A isAllowedNode() 0 13 5
1
<?php
2
3
namespace Goetas\TwitalBundle\EventSubscriber;
4
5
use Goetas\Twital\EventDispatcher\TemplateEvent;
6
use Goetas\Twital\Twital;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9
/**
10
 *
11
 * @author Asmir Mustafic <[email protected]>
12
 *
13
 */
14
class WhiteSpaceRemovalSubscriber implements EventSubscriberInterface
15
{
16
17
    protected $allowedTags = array();
18
19
    protected $allowedNamespaces = array();
20
21 23
    public function __construct(array $allowedTags = array(), array $allowedNamespaces = array())
22
    {
23 23
        $this->allowedTags = $allowedTags;
24 23
        $this->allowedNamespaces = $allowedNamespaces;
25 23
    }
26
27 18
    public static function getSubscribedEvents()
28
    {
29
        return array(
30 18
            'compiler.post_load' => array('removeWhitespace', -10)
31
        );
32
    }
33
34 23
    private function performXpathQuery(\DOMXPath $xp, $query, \DOMNode $ref)
35
    {
36 23
        if (defined('HHVM_VERSION')) {
37
            return $xp->query($query, $ref);
38
        } else {
39 23
            return $xp->query($query, $ref, false);
40
        }
41
    }
42
43 23
    public function removeWhitespace(TemplateEvent $event)
44
    {
45 23
        $doc = $event->getTemplate()->getDocument();
46
47 23
        $xp = new \DOMXPath($doc);
48 23
        $xp->registerNamespace("t", Twital::NS);
49
50 23
        foreach ($this->performXpathQuery($xp, "//text()[ancestor::*[@t:trans or @t:trans-n]]", $doc) as $text) {
51 12
            if ($this->isAllowedNode($text->parentNode)) {
52 5
                $text->data = preg_replace('/\s+/', ' ', $text->data);
53
54 5
                if ($text->parentNode->childNodes->length === 1) {
55 5
                    $trimmed = trim($text->data);
56 5
                    if (!strlen($trimmed) && strlen($text->data)) {
57 1
                        $trimmed = " ";
58
                    }
59 5
                    $text->data = $trimmed;
60 2
                } elseif ($text->parentNode->hasAttributeNs(Twital::NS, 'trans') || $text->parentNode->hasAttributeNs(Twital::NS, 'trans-n')) {
61 2
                    if ($text->parentNode->firstChild === $text) {
62 2
                        $text->data = ltrim($text->data);
63 2
                    } elseif ($text->parentNode->lastChild === $text) {
64 12
                        $text->data = rtrim($text->data);
65
                    }
66
                }
67
            }
68
        }
69 23
    }
70
71 12
    private function isAllowedNode(\DOMElement $element)
72
    {
73
74 12
        if ($this->allowedTags && !in_array($element->localName, $this->allowedTags)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->allowedTags of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
75
            return false;
76
        }
77
78 12
        if ($this->allowedNamespaces && !in_array($element->namespaceURI, $this->allowedNamespaces)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->allowedNamespaces of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
79 7
            return false;
80
        }
81
82 5
        return true;
83
    }
84
}
85