Completed
Pull Request — master (#12)
by Auke
34:04
created

NodeListTrait::parseArgs()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.3357

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 15
cts 19
cp 0.7895
rs 8.5906
cc 6
eloc 17
nc 4
nop 1
crap 6.3357
1
<?php
2
3
namespace arc\xml;
4
5
trait NodeListTrait {
6
7
    protected $writer = null;
8
9 1
    public function __construct( $list = null, $writer = null ) 
10
    {
11 1
        parent::__construct( $list );
12 1
        $this->writer = $writer;
13 1
    }
14
15 1
    public function __call( $name, $args ) 
16
    {
17 1
        $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 1
        list( $attributes, $content ) = $this->parseArgs( $args );
19 1
        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 1
        return $this;
21
    }
22
23 1
    public function __toString() 
24
    {
25 1
        $indent = '';
26 1
        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 1
        return join( $indent, (array) $this );
30
    }
31
32 1
    public static function indent( $indent, $content ) 
33
    {
34 1
        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 1
            return $content;
39
        }
40
    }
41
42 1
    private function parseArgs( $args ) 
43
    {
44 1
        $attributes = array();
45 1
        $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 1
        foreach ($args as $arg ) {
47 1
            if (is_string( $arg )) {
48 1
                $content .= $arg;
49 1
            } else if (is_array( $arg )) {
50 1
                foreach( $arg as $key => $subArg ) {
51 1
                    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 1
                        $attributes[ $key ] = $subArg;
57
                    }
58 1
                }
59 1
            } else {
60 1
                $content .= $arg;
61
            }
62 1
        }
63 1
        return [ $attributes, $content ];
64
    }
65
66 1
    protected function element( $tagName, $attributes, $content ) 
67
    {
68 1
        $tagName =  $this->writer->name( $tagName );
69 1
        $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 1
        $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 1
        if ($this->hasContent( $content )) {
72 1
            $el .=  '>' . self::indent( $this->writer->indent, $content );
73 1
            $el .= '</' . $tagName . '>';
74 1
        } else {
75
            $el .= '/>';
76
        }
77 1
        return $el;
78
    }
79
80 1
    protected function getAttributes( $attributes ) 
81
    {
82 1
        $result = '';
83 1
        if (count( $attributes )) {
84 1
            foreach ($attributes as $name => $value ) {
85 1
                $result .= ' ' . $this->writer->name( $name );
86 1
                $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 1
                if ($value !== $name ) {
88 1
                    $result .= '="' . $value . '"';
89 1
                }
90 1
            }
91 1
        }
92 1
        return $result;
93
    }
94
95 1
    protected function hasContent( $content ) 
96
    {
97 1
        return ( trim( $content ) != '' );
98
    }
99
100
}