Identifier   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 54
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sequential() 0 7 1
A random() 0 12 2
A tryApplyFallback() 0 7 3
1
<?php
2
namespace nstdio\svg\util;
3
4
use nstdio\svg\ElementInterface;
5
6
/**
7
 * Class Identifier
8
 *
9
 * @package nstdio\svg\util
10
 * @author  Edgar Asatryan <[email protected]>
11
 */
12
class Identifier
13
{
14
    const FALLBACK_LENGTH = 5;
15
16
    /**
17
     * @param ElementInterface $root
18
     * @param string           $tag
19
     *
20
     * @return string
21
     */
22 1
    public static function sequential(ElementInterface $root, $tag)
23
    {
24
25 1
        $count = $root->getRoot()->getElement()->getElementsByTagName($tag)->length;
0 ignored issues
show
Bug introduced by
The method getElement does only exist in nstdio\svg\container\ContainerInterface, but not in DOMDocument.

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...
26
27 1
        return $tag . (++$count);
28
    }
29
30
    /**
31
     * Generates random string with prepended `$prefix`.
32
     *
33
     * @param string $prefix The prefix of string.
34
     * @param int    $length The length of string.
35
     *
36
     * @return string
37
     */
38 58
    public static function random($prefix, $length)
39
    {
40 58
        self::tryApplyFallback($length);
41
42 58
        $prefix .= mt_rand(1, 9);
43 58
        $length--;
44 58
        for ($i = 0; $i < $length; $i++) {
45 58
            $prefix .= mt_rand(0, 9);
46 58
        }
47
48 58
        return $prefix;
49
    }
50
51
    /**
52
     * To avoid duplication.
53
     *
54
     * @param $length
55
     *
56
     * @return int
57
     */
58 58
    private static function tryApplyFallback(&$length)
59
    {
60 58
        $length = intval($length);
61 58
        if (!$length || $length <= 1) {
62 1
            $length = self::FALLBACK_LENGTH;
63 1
        }
64
    }
65
}