ItemFactory::build()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 3
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelLangBundler\BundleItems;
4
5
use Illuminate\Container\Container;
6
use LaravelLangBundler\Exceptions\InvalidModificationArgument;
7
8
class ItemFactory
9
{
10
    /**
11
     * Allowed values for BundleItem target property.
12
     *
13
     * @var array
14
     */
15
    const ALLOWEDTARGETS = [
16
        'value',
17
        'key',
18
        'both',
19
    ];
20
21
    /**
22
     * Build a BundleItem instance.
23
     *
24
     * @param string $id
25
     * @param string $type       Filename_affected
26
     * @param array  $parameters
27
     *
28
     * @return BundleItem
29
     */
30
    public static function build($id, $type = null, array $parameters = [])
31
    {
32
        if (is_null($type)) {
33
            return new BundleItem($id);
34
        }
35
36
        list($target, $name) = explode('_', $type);
37
38
        self::validateTarget($target);
39
40
        $className = ucfirst($name).'Mod';
41
42
        $appNamespace = Container::getInstance()->getNamespace();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Illuminate\Container\Container as the method getNamespace() does only exist in the following sub-classes of Illuminate\Container\Container: Illuminate\Foundation\Application. 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...
43
44
        $localClass = "\\{$appNamespace}LangBundler\\Mods\\{$className}";
45
46
        $vendorClass = "\LaravelLangBundler\\BundleItems\\Mods\\{$className}";
47
48
        if (class_exists($localClass)) {
49
            return new $localClass($id, $target, $parameters);
50
        } elseif (class_exists($vendorClass)) {
51
            return new $vendorClass($id, $target, $parameters);
52
        }
53
54
        throw InvalidModificationArgument::modifcationClassNotFound($className);
55
    }
56
57
    /**
58
     * Validate the target.
59
     *
60
     * @param string $target
61
     *
62
     * @throws InvalidModificationTarget
63
     */
64
    protected static function validateTarget($target)
65
    {
66
        if (!in_array($target, self::ALLOWEDTARGETS)) {
67
            throw InvalidModificationArgument::targetNotAllowed($target);
68
        }
69
    }
70
}
71