Completed
Push — master ( d90799...9daa28 )
by Алексей
12:20 queued 09:23
created

AnnotationHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAnnotations() 0 21 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: akeinhell
5
 * Date: 29.06.16
6
 * Time: 13:00
7
 */
8
9
namespace Telegram\Helpers;
10
11
use Jasny;
12
13
14
class AnnotationHelper
15
{
16
    private static $map = [
17
        'required' => 'boolean',
18
        'type'     => 'string',
19
    ];
20
21
    public static function getAnnotations($class, $value)
22
    {
23
        $propertyReflect = new \ReflectionProperty($class, $value);
24
        $doc             = $propertyReflect->getDocComment();
25
        if (!preg_match_all('#@(.*?)\n#s', $doc, $annotations)) {
26
            return false;
27
        };
28
        $annotations = $annotations[1];
29
        $return      = [];
30
        foreach ($annotations as $annotation) {
31
            list($k, $v) = array_pad(explode(' ', $annotation, 2), 2, null);
32
            if (array_key_exists($k, static::$map)) {
0 ignored issues
show
Bug introduced by
Since $map is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $map to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
33
                $type = static::$map[$k];
0 ignored issues
show
Bug introduced by
Since $map is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $map to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
34
                $v    = Jasny\TypeCast::value($v)->to($type);
35
            }
36
37
            $return[$k] = $v;
38
        }
39
40
        return $return;
41
    }
42
}