Completed
Push — master ( a178f7...102323 )
by Ryosuke
04:28
created

Utils::normalize()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 8.8571
cc 5
eloc 11
nc 8
nop 3
crap 5
1
<?php
2
3
namespace mpyw\Co\Internal;
4
5
use mpyw\Co\CoInterface;
6
use mpyw\Co\Internal\GeneratorContainer;
7
8
class Utils {
9
10
    /**
11
     * Recursively normalize value.
12
     *   Generator Closure  -> GeneratorContainer
13
     *   Array              -> Array (children's are normalized)
14
     *   Others             -> Others
15
     * @param  mixed    $value
16
     * @param  CoOption $options
17
     * @param  mixed    $yield_key
18
     * @return mixed
19
     */
20
    public static function normalize($value, CoOption $options, $yield_key = null)
21
    {
22 16
        if (self::isGeneratorClosure($value)) {
23 16
            $value = $value();
24 16
        }
25
        if ($value instanceof \Generator) {
26 16
            return new GeneratorContainer($value, $options, $yield_key);
27 7
        }
28 5
        if (is_array($value)) {
29 5
            $tmp = [];
30 4
            foreach ($value as $k => $v) {
31
                $tmp[$k] = self::normalize($v, $options, $yield_key);
32
            }
33
            return $tmp;
34 16
        }
35 16
        return $value;
36
    }
37 13
38 9
    /**
39 9
     * Recursively search yieldable values.
40 9
     * Each entries are assoc those contain keys 'value' and 'keylist'.
41
     *   value   -> the value itself.
42 8
     *   keylist -> position of the value. nests are represented as array values.
43
     * @param  mixed $value   Must be already normalized.
44 13
     * @param  array $keylist Internally used.
45
     * @return array
46
     */
47
    public static function getYieldables($value, array $keylist = [])
48
    {
49
        $r = [];
50
        if (!is_array($value)) {
51
            if (self::isCurl($value) || self::isGeneratorContainer($value)) {
52
                $r[(string)$value] = [
53
                    'value' => $value,
54
                    'keylist' => $keylist,
55
                ];
56 13
            }
57 13
            return $r;
58 13
        }
59 13
        foreach ($value as $k => $v) {
60 13
            $newlist = array_merge($keylist, [$k]);
61 11
            $r = array_merge($r, self::getYieldables($v, $newlist));
62 11
        }
63 11
        return $r;
64
    }
65
66 13
    /**
67
     * Check if value is a valid cURL handle.
68 8
     * @param  mixed $value
69 8
     * @return bool
70 8
     */
71
    public static function isCurl($value)
72 8
    {
73
        return is_resource($value) && get_resource_type($value) === 'curl';
74
    }
75
76
    /**
77
     * Check if value is a valid Generator.
78
     * @param  mixed $value
79
     * @return bool
80 14
     */
81 14
    public static function isGeneratorContainer($value)
82 2
    {
83
        return $value instanceof GeneratorContainer;
84
    }
85
86
    /**
87
     * Check if value is a valid Generator closure.
88
     * @param  mixed $value
89
     * @return bool
90 14
     */
91 14
    public static function isGeneratorClosure($value)
92 14
    {
93
        return $value instanceof \Closure
94
            && (new \ReflectionFunction($value))->isGenerator();
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ReflectionFunction as the method isGenerator() does only exist in the following sub-classes of ReflectionFunction: Go\ParserReflection\ReflectionFunction. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
95
    }
96
97
}
98