Completed
Push — master ( 9b0dae...9811d3 )
by Thomas
03:40
created

XMLWriter::applyNamespaces()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace FluentDOM {
4
5
  use FluentDOM\XMLWriter\NamespaceDefinition;
6
  use FluentDOM\XMLWriter\NamespaceStack;
7
8
  class XMLWriter extends \XMLWriter {
9
10
    /**
11
     * @var Namespaces
12
     */
13
    private $_namespaces;
14
15
    /**
16
     * @var XMLWriter\NamespaceStack
17
     */
18
    private $_xmlnsStack;
19
20 3
    public function __construct() {
21 3
      $this->_namespaces = new Namespaces();
22 3
      $this->_xmlnsStack = new XMLWriter\NamespaceStack();
23 3
    }
24
25
    /**
26
     * register a namespace prefix for the xml reader, it will be used in
27
     * next() and other methods with a tag name argument
28
     *
29
     * @param string $prefix
30
     * @param string $namespace
31
     * @throws \LogicException
32
     */
33 2
    public function registerNamespace($prefix, $namespace) {
34 2
      $this->_namespaces[$prefix] = $namespace;
35 2
    }
36
37
    /**
38
     * Add the current namespace configuration as xmlns* attributes to the element node.
39
     */
40 1
    public function applyNamespaces() {
41 1
      foreach ($this->_namespaces as $prefix => $namespaceUri) {
42 1
        $this->writeAttribute(
43 1
          empty($prefix) || $prefix == '#default' ? 'xmlns' : 'xmlns:'.$prefix, $namespaceUri
44 1
        );
45 1
      }
46 1
    }
47
48 3
    public function startElement($name) {
49 3
      list($prefix, $localName) = QualifiedName::split($name);
50 3
      $namespaceUri = $this->_namespaces->resolveNamespace($prefix);
0 ignored issues
show
Security Bug introduced by
It seems like $prefix defined by \FluentDOM\QualifiedName::split($name) on line 49 can also be of type false; however, FluentDOM\Namespaces::resolveNamespace() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
51 3
      return $this->startElementNS((string)$prefix, $localName, $namespaceUri);
52
    }
53
54 2
    public function writeElement($name, $content = NULL) {
55 2
      list($prefix, $localName) = QualifiedName::split($name);
56 2
      $namespaceUri = $this->_namespaces->resolveNamespace($prefix);
0 ignored issues
show
Security Bug introduced by
It seems like $prefix defined by \FluentDOM\QualifiedName::split($name) on line 55 can also be of type false; however, FluentDOM\Namespaces::resolveNamespace() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
57 2
      return $this->writeElementNS((string)$prefix, $localName, $namespaceUri, $content);
58
    }
59
60 3
    public function startElementNS($prefix, $name, $namespaceUri) {
61 3
      $this->_xmlnsStack->push();
62 3 View Code Duplication
      if ($this->_xmlnsStack->isDefined($prefix, $namespaceUri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63 3
        parent::startElement(empty($prefix) ? $name : $prefix.':'.$name);
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (startElement() instead of startElementNS()). Are you sure this is correct? If so, you might want to change this to $this->startElement().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
64 3
      } else {
65 2
        parent::startElementNS(empty($prefix) ? NULL : $prefix, $name, $namespaceUri);
66 2
        $this->_xmlnsStack->add($prefix, $namespaceUri);
67
      }
68 3
    }
69
70 2
    public function writeElementNS($prefix, $name, $uri, $content = NULL) {
71 2 View Code Duplication
      if ($this->_xmlnsStack->isDefined($prefix, $uri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72 2
        parent::writeElement(empty($prefix) ? $name : $prefix.':'.$name, $content);
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (writeElement() instead of writeElementNS()). Are you sure this is correct? If so, you might want to change this to $this->writeElement().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
73 2
      } else {
74
        parent::writeElementNS(empty($prefix) ? NULL : $prefix, $name, $uri, $content);
75
      }
76 2
    }
77
78 3
    public function endElement() {
79 3
      $this->_xmlnsStack->pop();
80 3
      parent::endElement();
81 3
    }
82
83
    public function startAttribute($name) {
84
      list($prefix, $localName) = QualifiedName::split($name);
85
      $this->startAttributeNS($prefix, $localName, $this->_namespaces->resolveNamespace($prefix));
0 ignored issues
show
Security Bug introduced by
It seems like $prefix defined by \FluentDOM\QualifiedName::split($name) on line 84 can also be of type false; however, FluentDOM\Namespaces::resolveNamespace() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
86
    }
87
88 2
    public function writeAttribute($name, $value) {
89 2
      list($prefix, $localName) = QualifiedName::split($name);
90 2
      $this->writeAttributeNS($prefix, $localName, $this->_namespaces->resolveNamespace($prefix), $value);
0 ignored issues
show
Security Bug introduced by
It seems like $prefix defined by \FluentDOM\QualifiedName::split($name) on line 89 can also be of type false; however, FluentDOM\Namespaces::resolveNamespace() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
91 2
    }
92
93
    public function startAttributeNS($prefix, $name, $uri) {
94
      if (empty($prefix)) {
95
        parent::startAttribute($name);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (startAttribute() instead of startAttributeNS()). Are you sure this is correct? If so, you might want to change this to $this->startAttribute().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
96
      } elseif ($this->_xmlnsStack->isDefined($prefix, $uri)) {
97
        parent::startAttribute($prefix.':'.$name);
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (startAttribute() instead of startAttributeNS()). Are you sure this is correct? If so, you might want to change this to $this->startAttribute().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
98
      } else {
99
        parent::startAttributeNS($prefix, $name, $uri);
100
      }
101
    }
102
103 2
    public function writeAttributeNS($prefix, $localName, $uri, $content) {
104 2
      if ((empty($prefix) && $localName == 'xmlns') || $prefix == 'xmlns') {
105 1
        $namespacePrefix = empty($prefix) ? '' : $localName;
106 1
        $namespaceUri = $content;
107 1
        if (!$this->_xmlnsStack->isDefined($namespacePrefix, $namespaceUri)) {
108 1
          parent::writeAttribute(empty($prefix) ? 'xmlns' : 'xmlns:'.$localName, $namespaceUri);
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (writeAttribute() instead of writeAttributeNS()). Are you sure this is correct? If so, you might want to change this to $this->writeAttribute().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
109 1
          $this->_xmlnsStack->add($namespacePrefix, $namespaceUri);
110 1
        }
111 2
      } elseif (empty($prefix)) {
112 2
        parent::writeAttribute($localName, $content);
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (writeAttribute() instead of writeAttributeNS()). Are you sure this is correct? If so, you might want to change this to $this->writeAttribute().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
113 2
      } elseif ($this->_xmlnsStack->isDefined($prefix, $uri)) {
114 1
        parent::writeAttribute($prefix.':'.$localName, $content);
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (writeAttribute() instead of writeAttributeNS()). Are you sure this is correct? If so, you might want to change this to $this->writeAttribute().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
115 1
      } else {
116
        parent::writeAttributeNS($prefix, $localName, $uri, $content);
117
      }
118 2
    }
119
  }
120
}