HasNameTrait::getAliases()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Properties\AbstractProperty\Traits;
4
5
use ReflectionClass;
6
7
/**
8
 * Trait HasNameTrait
9
 * @package ByTIC\Models\SmartProperties\Properties\AbstractProperty\Traits
10
 */
11
trait HasNameTrait
12
{
13
    protected $namespace = null;
14
15
    protected $aliases = [];
16
17
    /**
18 15
     * @return array
19
     */
20 15
    public function getAliases(): array
21 15
    {
22
        return array_merge($this->aliases, [$this->generateNameFromClass()]);
23
    }
24 15
25
    /**
26
     * @return null
27 15
     */
28
    public function getNamespace()
29 15
    {
30 15
        return $this->namespace;
31
    }
32
33
    /**
34
     * @param null $namespace
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $namespace is correct as it would always require null to be passed?
Loading history...
35 15
     */
36
    public function setNamespace($namespace): void
37 15
    {
38 15
        $this->namespace = $namespace;
39
    }
40 15
41
    /**
42
     * @return string
43
     */
44
    protected function generateName(): string
45
    {
46 15
        $name = $this->generateNameFromClass();
47
        $name = inflector()->unclassify($name);
48
49 15
        return $name;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    protected function generateNameFromClass(): string
56
    {
57
        $class = static::class;
58
        $base = $this->getNamespace();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $base is correct as $this->getNamespace() targeting ByTIC\Models\SmartProper...meTrait::getNamespace() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
59
        if ($base && strpos($class, $base) ===0) {
0 ignored issues
show
Bug introduced by
$base of type void is incompatible with the type string expected by parameter $needle of strpos(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        if ($base && strpos($class, /** @scrutinizer ignore-type */ $base) ===0) {
Loading history...
introduced by
$base is of type null, thus it always evaluated to false.
Loading history...
60
            return str_replace($base, '', $class);
61
        }
62
        try {
63
            return (new ReflectionClass($this))->getShortName();
64
        } catch (\ReflectionException $e) {
65
            return '';
66
        }
67
    }
68
}
69