GetPropertiesExtension::exludeAndReplace()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 17
nc 6
nop 4
1
<?php
2
namespace Hospitalplugin\Twig;
3
4
class GetPropertiesExtension extends \Twig_Extension
5
{
6
7
    public function getFilters()
8
    {
9
        return array(
10
            new \Twig_SimpleFilter('props', array(
11
                $this,
12
                'getPropsFilter'
13
            ))
14
        );
15
    }
16
17
    /**
18
     * return props of a class
19
     * @param unknown $class
20
     * @return boolean|multitype:
0 ignored issues
show
Documentation introduced by
The doc-type boolean|multitype: could not be parsed: Unknown type name "multitype:" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
21
     */
22
    private static function getProps($class)
23
    {
24
        if ($class == NULL) {
25
            return array();
26
        }
27
        $class = new \ReflectionClass($class);
28
        $properties = array_filter($class->getProperties(), function ($prop) use($class)
29
        {
30
            return $prop->getDeclaringClass()->name == $class->name;
31
        });
32
        return $properties;
33
    }
34
35
    private static function exludeAndReplace($properties, $exclude, $replace, $titles)
36
    {
37
        $propsArray = [];
38
        foreach ($properties as $value) {
39
            if (in_array($value->name, $exclude)) {
40
                continue;
41
            }
42
            if (array_key_exists($value->name, $replace)) {
43
                $name = $replace[$value->name];
44
            } else {
45
                $name = $value->name;
46
            }
47
            if (array_key_exists($value->name, $titles)) {
48
                $title = $titles[$value->name];
49
            } else {
50
                $title = strtoupper(substr($name, 0, 1));
51
            }
52
            array_push($propsArray, array(
53
                'data' => $name,
54
                'title' => $title
55
            ));
56
        }
57
        return $propsArray;
58
    }
59
60
    /**
61
     * returns properties of an object excluding those that have name same as in $exclude array
62
     * 
63
     * order of returned properties is: first parent class of $obj props then base class props of an $obj
64
     * 
65
     * @param unknown $obj
66
     * @param unknown $exclude
67
     * @return boolean|string
68
     */
69
    public function getPropsFilter($obj, $exclude, $replace, $titles)
70
    {
71
        // properties of an object, not array
72
        while (is_array($obj)) {
73
            $obj = $obj[0];
74
        }
75
        $parentProperties = GetPropertiesExtension::getProps(get_parent_class($obj));
0 ignored issues
show
Documentation introduced by
get_parent_class($obj) is of type string, but the function expects a object<Hospitalplugin\Twig\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        $properties = GetPropertiesExtension::getProps($obj);
77
        $propertiesMerged = array_merge($parentProperties, $properties);
78
        $propsArray = GetPropertiesExtension::exludeAndReplace($propertiesMerged, $exclude, $replace, $titles);
79
        return $propsArray;
80
    }
81
82
    public function getName()
83
    {
84
        return 'get_properties_extension';
85
    }
86
}