Descriptive::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace nstdio\svg\desc;
3
use nstdio\svg\ElementInterface;
4
use nstdio\svg\SVGElement;
5
6
/**
7
 * Class Descriptive
8
 *
9
 * @package nstdio\svg\desc
10
 * @author  Edgar Asatryan <[email protected]>
11
 */
12
abstract class Descriptive extends SVGElement
13
{
14 3
    public function __construct(ElementInterface $parent, $value)
15
    {
16 3
        parent::__construct($parent);
17
18 3
        $this->getElement()->setNodeValue($value);
0 ignored issues
show
Bug introduced by
The method setNodeValue does only exist in nstdio\svg\XMLDocumentInterface, but not in DOMElement and nstdio\sv...iner\ContainerInterface.

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...
19
    }
20
}