Completed
Push — travis ( f73ca9 )
by Asmir
03:03
created

WhiteSpaceRemovalSubscriber   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 71
ccs 38
cts 40
cp 0.95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A performXpathQuery() 0 8 2
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
D removeWhitespace() 0 27 10
B isAllowedNode() 0 13 5
1
<?php
2
namespace Goetas\TwitalBundle\EventSubscriber;
3
4
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
5
use Goetas\Twital\EventDispatcher\TemplateEvent;
6
use Goetas\Twital\Twital;
7
8
/**
9
 *
10
 * @author Asmir Mustafic <[email protected]>
11
 *
12
 */
13
class WhiteSpaceRemovalSubscriber implements EventSubscriberInterface
14
{
15
16
    protected $allowedTags = array();
17
18
    protected $allowedNamespaces = array();
19
20 23
    public function __construct(array $allowedTags = array(), array $allowedNamespaces = array())
21
    {
22 23
        $this->allowedTags = $allowedTags;
23 23
        $this->allowedNamespaces = $allowedNamespaces;
24 23
    }
25
26 18
    public static function getSubscribedEvents()
27
    {
28
        return array(
29 18
            'compiler.post_load' => array('removeWhitespace', -10)
30 18
        );
31
    }
32
33 23
    private function performXpathQuery(\DOMXPath $xp, $query, \DOMNode $ref)
34
    {
35 23
        if (defined('HHVM_VERSION')) {
36
            return $xp->query($query, $ref);
37
        } else {
38 23
            return $xp->query($query, $ref, false);
39
        }
40
    }
41
42 23
    public function removeWhitespace(TemplateEvent $event)
43
    {
44 23
        $doc = $event->getTemplate()->getDocument();
45
46 23
        $xp = new \DOMXPath($doc);
47 23
        $xp->registerNamespace("t", Twital::NS);
48
49 23
        foreach ($this->performXpathQuery($xp, "//text()[ancestor::*[@t:trans or @t:trans-n]]", $doc) as $text) {
50 12
            if ($this->isAllowedNode($text->parentNode)) {
51 5
                $text->data = preg_replace('/\s+/', ' ', $text->data);
52
53 5
                if ($text->parentNode->childNodes->length === 1) {
54 5
                    $trimmed = trim($text->data);
55 5
                    if (!strlen($trimmed) && strlen($text->data)) {
56 1
                        $trimmed = " ";
57 1
                    }
58 5
                    $text->data = $trimmed;
59 5
                } elseif ($text->parentNode->hasAttributeNs(Twital::NS, 'trans') || $text->parentNode->hasAttributeNs(Twital::NS, 'trans-n')) {
60 2
                    if ($text->parentNode->firstChild === $text) {
61 2
                        $text->data = ltrim($text->data);
62 2
                    } elseif ($text->parentNode->lastChild === $text) {
63 2
                        $text->data = rtrim($text->data);
64 2
                    }
65 2
                }
66 5
            }
67 23
        }
68 23
    }
69
70 12
    private function isAllowedNode(\DOMElement $element)
71
    {
72
73 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...
74
            return false;
75
        }
76
77 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...
78 7
            return false;
79
        }
80
81 5
        return true;
82
    }
83
}
84