ElementTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 30
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A append() 0 20 4
1
<?php
2
namespace nstdio\svg\traits;
3
4
use nstdio\svg\ElementInterface;
5
use nstdio\svg\XMLDocumentInterface;
6
7
/**
8
 * Trait ElementTrait
9
 *
10
 * @package nstdio\svg\traits
11
 * @author  Edgar Asatryan <[email protected]>
12
 */
13
trait ElementTrait
14
{
15
    /**
16
     * Multiply argument can be passed.
17
     *
18
     * @param ElementInterface $elements
19
     *
20
     * @return $this
21
     */
22
    public function append(ElementInterface $elements)
0 ignored issues
show
Unused Code introduced by
The parameter $elements is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24
        /** @var ElementInterface[] $elements */
25 150
        $elements = array_filter(func_get_args(), function($item) {
26 150
            return $item instanceof ElementInterface;
27 150
        });
28
29 150
        foreach ($elements as $element) {
30 150
            $this->child[] = $element;
0 ignored issues
show
Bug introduced by
The property child does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
            /** @var \DOMElement $e */
32 150
            $e = $this->getElement();
0 ignored issues
show
Bug introduced by
It seems like getElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33 150
            if ($e instanceof \DOMNode) {
34 150
                $e->appendChild($element->getElement()->getElement());
0 ignored issues
show
Bug introduced by
The method getElement does only exist in nstdio\svg\XMLDocumentIn...iner\ContainerInterface, but not in DOMElement.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
35 150
            } elseif ($e instanceof XMLDocumentInterface) {
36 75
                $e->appendChild($element->getElement());
37 75
            }
38 150
        }
39
40 150
        return $this;
41
    }
42
}