Completed
Push — master ( c719ff...c812d0 )
by Auke
101:35 queued 98:19
created

NodeListTrait::getAttributes()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.2
cc 4
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace arc\xml;
4
5
trait NodeListTrait {
6
7
    protected $writer = null;
8
9
    public function __construct( $list = null, $writer = null ) 
10
    {
11
        parent::__construct( $list );
12
        $this->writer = $writer;
13
    }
14
15
    public function __call( $name, $args ) 
16
    {
17
        $tagName = $name;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 22 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
18
        list( $attributes, $content ) = $this->parseArgs( $args );
19
        parent::offsetSet( null, $this->element( $tagName, $attributes, $content ) );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetSet() instead of __call()). Are you sure this is correct? If so, you might want to change this to $this->offsetSet().

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...
20
        return $this;
21
    }
22
23
    public function __toString() 
24
    {
25
        $indent = '';
26
        if (!is_object( $this->writer ) || $this->writer->indent ) {
27
            $indent = "\r\n"; // element() will indent each line with whatever indent string is in the writer
28
        }
29
        return join( $indent, (array) $this );
30
    }
31
32
    public static function indent( $indent, $content ) 
33
    {
34
        if ($indent && ( strpos( $content, '<' ) !== false )) {
35
            $indent = ( is_string( $indent ) ? $indent : "\t" );
36
            return "\r\n" . preg_replace( '/^(\s*)</m', $indent.'$1<', $content ) . "\r\n";
37
        } else {
38
            return $content;
39
        }
40
    }
41
42
    private function parseArgs( $args ) 
43
    {
44
        $attributes = array();
45
        $content = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
46
        foreach ($args as $arg ) {
47
            if (is_string( $arg )) {
48
                $content .= $arg;
49
            } else if (is_array( $arg )) {
50
                foreach( $arg as $key => $subArg ) {
51
                    if (is_numeric( $key )) {
52
                        list( $subattributes, $subcontent ) = $this->parseArgs( $subArg );
53
                        $attributes = $subattributes + $attributes;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 25 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
                        $content .= $subcontent;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 27 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
                    } else {
56
                        $attributes[ $key ] = $subArg;
57
                    }
58
                }
59
            } else {
60
                $content .= $arg;
61
            }
62
        }
63
        return [ $attributes, $content ];
64
    }
65
66
    protected function element( $tagName, $attributes, $content ) 
67
    {
68
        $tagName =  $this->writer->name( $tagName );
69
        $el = '<' . $tagName;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
70
        $el .= $this->getAttributes( $attributes );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71
        if ($this->hasContent( $content )) {
72
            $el .=  '>' . self::indent( $this->writer->indent, $content );
73
            $el .= '</' . $tagName . '>';
74
        } else {
75
            $el .= '/>';
76
        }
77
        return $el;
78
    }
79
80
    protected function getAttributes( $attributes ) 
81
    {
82
        $result = '';
83
        if (count( $attributes )) {
84
            foreach ($attributes as $name => $value ) {
85
                $result .= ' ' . $this->writer->name( $name );
86
                $value = $this->writer->value( $value );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
87
                if ($value !== $name ) {
88
                    $result .= '="' . $value . '"';
89
                }
90
            }
91
        }
92
        return $result;
93
    }
94
95
    protected function hasContent( $content ) 
96
    {
97
        return ( trim( $content ) != '' );
98
    }
99
100
}