Passed
Pull Request — 2.x (#1376)
by Harings
09:59
created

UpdateExampleCommand::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use A17\Twill\Commands\Traits\HandlesPresets;
6
use Illuminate\Console\Command;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, A17\Twill\Commands\Command. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
class UpdateExampleCommand extends Command
9
{
10
    use HandlesPresets;
11
12
    protected $signature = 'twill:update-example {preset}';
13
14
    protected $description = 'Updates the twill examples folder';
15
16
    public function handle(): void
17
    {
18
        $preset = $this->argument('preset');
19
20
        if ($this->presetExists($preset)) {
0 ignored issues
show
Bug introduced by
It seems like $preset can also be of type array and null; however, parameter $preset of A17\Twill\Commands\Updat...Command::presetExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
        if ($this->presetExists(/** @scrutinizer ignore-type */ $preset)) {
Loading history...
21
            if ($this->confirm('Are you sure to update this preset?')) {
22
                $this->updatePreset($preset);
0 ignored issues
show
Bug introduced by
It seems like $preset can also be of type array and null; however, parameter $preset of A17\Twill\Commands\Updat...Command::updatePreset() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
                $this->updatePreset(/** @scrutinizer ignore-type */ $preset);
Loading history...
23
            } else {
24
                $this->warn('Cancelled.');
25
            }
26
        } else {
27
            $this->error("Could not find preset: $preset");
28
        }
29
    }
30
}
31