ComposerCommandProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommands() 0 4 1
A setCommands() 0 6 1
A areCommands() 0 10 3
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Commands;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Composer\Command\BaseCommand;
22
use Composer\Plugin\Capability\CommandProvider;
23
use function assert;
24
25
/**
26
 * @package Limoncello\Commands
27
 */
28
class ComposerCommandProvider implements CommandProvider
29
{
30
    /**
31
     * @var BaseCommand[]
32
     */
33
    private static $commands = [];
34
35 1
    /**
36
     * @inheritdoc
37 1
     */
38
    public function getCommands()
39
    {
40
        return static::$commands;
0 ignored issues
show
Bug introduced by
Since $commands is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $commands to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
41
    }
42
43
    /**
44
     * @param BaseCommand[] $commands
45
     *
46
     * @return void
47 1
     *
48
     * @SuppressWarnings(PHPMD.StaticAccess)
49 1
     */
50
    public static function setCommands(array $commands): void
51 1
    {
52
        assert(static::areCommands($commands) === true);
0 ignored issues
show
Bug introduced by
Since areCommands() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of areCommands() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
53
54
        static::$commands = $commands;
0 ignored issues
show
Bug introduced by
Since $commands is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $commands to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
55
    }
56
57
    /**
58
     * @param array $commands
59 1
     *
60
     * @return bool
61 1
     */
62
    private static function areCommands(array $commands): bool
63 1
    {
64 1
        $areCommands = true;
65
66
        foreach ($commands as $command) {
67 1
            $areCommands = $areCommands && ($command instanceof BaseCommand);
68
        }
69
70
        return $areCommands;
71
    }
72
}
73