Completed
Branch master (42e482)
by Edgar
05:48 queued 02:06
created

Identifier::random()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
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
    public static function sequential(ElementInterface $root, $tag)
17
    {
18
        $count = $root->getRoot()->getElement()->getElementsByTagName($tag)->length;
0 ignored issues
show
Bug introduced by
The method getElement() does not exist on DOMElement. Did you maybe mean getElementsByTagName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
19
20
        return $tag . (++$count);
21
    }
22
23
    public static function random($prefix, $length)
24
    {
25
        self::tryApplyFallback($length);
26
27
        $prefix .= mt_rand(1, 9);
28
        $length--;
29
        for ($i = 0; $i < $length; $i++) {
30
            $prefix .= mt_rand(0, 9);
31
        }
32
33
        return $prefix;
34
    }
35
36
    /**
37
     * @param $length
38
     *
39
     * @return int
40
     */
41
    private static function tryApplyFallback(&$length)
42
    {
43
        $length = intval($length);
44
        if (!$length || $length <= 1) {
45
            $length = self::FALLBACK_LENGTH;
46
        }
47
    }
48
}