Completed
Push — master ( ebcbe7...06aa7a )
by Ryosuke
04:11
created

TypeUtils::isCurl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace mpyw\Co\Internal;
4
5
class TypeUtils
6
{
7
    /**
8
     * Check if value is a valid cURL handle.
9
     * @param  mixed $value
10
     * @return bool
11
     */
12 32
    public static function isCurl($value)
13 32
    {
14 2
        return is_resource($value) && get_resource_type($value) === 'curl';
15
    }
16
17
    /**
18
     * Check if value is a valid Generator.
19
     * @param  mixed $value
20
     * @return bool
21
     */
22 29
    public static function isGeneratorContainer($value)
23 29
    {
24 29
        return $value instanceof GeneratorContainer;
25
    }
26
27
    /**
28
     * Check if value is a valid Generator closure.
29
     * @param  mixed $value
30
     * @return bool
31
     */
32 33
    public static function isGeneratorClosure($value)
33 33
    {
34 33
        return $value instanceof \Closure
35 33
            && (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...
36
    }
37
}
38