ContentItem::convert()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 14
ccs 11
cts 11
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\PageBundle\Entity;
7
8
use Zicht\Util\Str;
9
use Zicht\Bundle\PageBundle\Model\ContentItemInterface;
10
11
/**
12
 * Base class for ContentItem entities.
13
 */
14
abstract class ContentItem implements ContentItemInterface
15
{
16
    /**
17
     * Returns a (dash notated) short type that can be used in databases, css classes, etc.
18
     *
19
     * @param string $infix
20
     * @return string
21
     */
22 1
    public function getShortType($infix = '-')
23
    {
24 1
        $shortType = Str::infix(lcfirst(Str::classname(Str::rstrip($this->getType(), 'ContentItem'))), $infix);
25
26 1
        if ($template = $this->getTemplateName()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $template is correct as $this->getTemplateName() targeting Zicht\Bundle\PageBundle\...Item::getTemplateName() 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...
27
            $shortType .= $infix . $template;
0 ignored issues
show
Bug introduced by
Are you sure $template of type void can be used in concatenation? ( Ignorable by Annotation )

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

27
            $shortType .= $infix . /** @scrutinizer ignore-type */ $template;
Loading history...
28
        }
29
30 1
        return $shortType;
31
    }
32
33
    /**
34
     * Copies all properties from the source to the target, if the target has the specified properties.
35
     *
36
     * @param ContentItemInterface $from
37
     * @param ContentItemInterface $to
38
     * @return ContentItemInterface
39
     */
40 1
    public static function convert(ContentItemInterface $from, ContentItemInterface $to)
41
    {
42 1
        $reflectionFrom = new \ReflectionClass($from);
43 1
        $reflectionTo = new \ReflectionClass($to);
44
45 1
        foreach ($reflectionFrom->getProperties() as $property) {
46 1
            $property->setAccessible(true);
47 1
            $method = 'set' . ucfirst($property->getName());
48 1
            if ($reflectionTo->hasMethod($method)) {
49 1
                $to->$method($property->getValue($from));
50 1
            }
51 1
        }
52
53 1
        return $to;
54
    }
55
56
    /**
57
     * Returns the type of the class.
58
     *
59
     * @return string
60
     */
61 1
    final public function getType()
62
    {
63 1
        return get_class($this);
64
    }
65
66
67
    protected $convertToType = null;
68
69
    /**
70
     * Return string
71
     *
72
     * @return string
73
     */
74
    final public function getConvertToType()
75
    {
76
        if (null !== $this->convertToType) {
77
            return $this->convertToType;
78
        }
79
        return $this->getType();
80
    }
81
82
83
    /**
84
     * Set the type to convert to.
85
     *
86
     * @param string $type
87
     * @return void
88
     */
89 1
    public function setConvertToType($type)
90
    {
91 1
        $this->convertToType = $type;
92 1
    }
93
94
    /**
95
     * If needed, you can add a custom template name to the content item
96
     *
97
     * @return null|string
98
     */
99 1
    public function getTemplateName()
100
    {
101 1
        return null;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getInternalName()
108
    {
109
        return (string)$this;
110
    }
111
112
    /**
113
     * get region of content item, for BC just
114
     * setting as empty place holder.
115
     *
116
     * @return string
117
     */
118
    public function getRegion()
119
    {
120
    }
121
}
122