MakeBundleMod::getStub()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelLangBundler\Commands;
4
5
use Illuminate\Container\Container;
6
7
class MakeBundleMod extends LaravelLangBundlerCommand
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'langb:mod {name}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Create a custom modification file.';
22
23
    /**
24
     * Execute the console command.
25
     */
26
    public function handle()
27
    {
28
        $this->setUp();
29
30
        $pathArray = ['LangBundler', 'Mods'];
31
32
        $basePath = app_path().DIRECTORY_SEPARATOR;
33
34
        $this->buildPath($pathArray, $basePath);
35
36
        $name = ucfirst($this->argument('name')).'Mod';
37
38
        $stub = $this->getStub($name);
39
40
        $this->filesystem->put(
41
            app_path(implode(DIRECTORY_SEPARATOR, $pathArray).DIRECTORY_SEPARATOR.$name.'.php'),
42
            $stub
43
        );
44
45
        $this->info('New bundle modification file successfully created!');
46
    }
47
48
    /**
49
     * Get stub and fill.
50
     *
51
     * @param string $name
52
     *
53
     * @return string
54
     */
55
    protected function getStub($name)
56
    {
57
        $namespace = 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...
58
59
        $namespace = $namespace.'LangBundler\\Mods';
60
61
        $stub = $this->filesystem->get(
62
            dirname(__DIR__).DIRECTORY_SEPARATOR.'BundleItems'.DIRECTORY_SEPARATOR.'ModStub.php');
63
64
        $stub = str_replace(
65
            'LaravelLangBundler\BundleItems',
66
            $namespace,
67
            $stub
68
        );
69
70
        $stub = str_replace(
71
            'ModStub',
72
            $name,
73
            $stub
74
        );
75
76
        return $stub;
77
    }
78
}
79