Completed
Push — master ( 6e829b...9bdc49 )
by Greg
01:23
created

SiteAliasCommands::getLocationsAndAliasName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Consolidation\SiteAlias\Cli;
4
5
use Consolidation\SiteAlias\SiteAliasFileLoader;
6
use Consolidation\SiteAlias\SiteAliasManager;
7
use Consolidation\SiteAlias\Util\YamlDataFileLoader;
8
use Consolidation\SiteAlias\SiteSpecParser;
9
use Consolidation\SiteAlias\SiteAliasName;
10
11
class SiteAliasCommands extends \Robo\Tasks
12
{
13
    protected $aliasLoader;
14
15
    /**
16
     * List available site aliases.
17
     *
18
     * @command site:list
19
     * @format yaml
20
     * @return array
21
     */
22
    public function siteList(array $varArgs)
23
    {
24
        $this->aliasLoader = new SiteAliasFileLoader();
25
        $ymlLoader = new YamlDataFileLoader();
26
        $this->aliasLoader->addLoader('yml', $ymlLoader);
27
        $aliasName = $this->getLocationsAndAliasName($varArgs, $this->aliasLoader);
0 ignored issues
show
Unused Code introduced by
The call to SiteAliasCommands::getLocationsAndAliasName() has too many arguments starting with $this->aliasLoader.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28
29
        $this->manager = new SiteAliasManager($this->aliasLoader);
0 ignored issues
show
Bug introduced by
The property manager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
31
        return $this->renderAliases($this->manager->getMultiple($aliasName));
32
    }
33
34
    /**
35
     * Load available site aliases.
36
     *
37
     * @command site:load
38
     * @format yaml
39
     * @return array
40
     */
41
    public function siteLoad(array $dirs)
42
    {
43
        $this->aliasLoader = new SiteAliasFileLoader();
44
        $ymlLoader = new YamlDataFileLoader();
45
        $this->aliasLoader->addLoader('yml', $ymlLoader);
46
47
        foreach ($dirs as $dir) {
48
            $this->io()->note("Add search location: $dir");
49
            $this->aliasLoader->addSearchLocation($dir);
50
        }
51
52
        $all = $this->aliasLoader->loadAll();
53
54
        return $this->renderAliases($all);
55
    }
56
57
    protected function getLocationsAndAliasName($varArgs)
58
    {
59
        $aliasName = '';
60
        foreach ($varArgs as $arg) {
61
            if (SiteAliasName::isAliasName($arg)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \Consolidation\SiteAlias...Name::isAliasName($arg) of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
62
                $this->io()->note("Alias parameter: '$arg'");
63
                $aliasName = $arg;
64
            } else {
65
                $this->io()->note("Add search location: $arg");
66
                $this->aliasLoader->addSearchLocation($arg);
67
            }
68
        }
69
        return $aliasName;
70
    }
71
72
    protected function renderAliases($all)
73
    {
74
        if (empty($all)) {
75
            throw new \Exception("No aliases found");
76
        }
77
78
        $result = [];
79
        foreach ($all as $name => $alias) {
80
            $result[$name] = $alias->export();
81
        }
82
83
        return $result;
84
    }
85
86
    /**
87
     * List available site aliases.
88
     *
89
     * @command site:get
90
     * @format yaml
91
     * @return array
92
     */
93
    public function siteGet(array $varArgs)
94
    {
95
        $this->aliasLoader = new SiteAliasFileLoader();
96
        $ymlLoader = new YamlDataFileLoader();
97
        $this->aliasLoader->addLoader('yml', $ymlLoader);
98
        $aliasName = $this->getLocationsAndAliasName($varArgs, $this->aliasLoader);
0 ignored issues
show
Unused Code introduced by
The call to SiteAliasCommands::getLocationsAndAliasName() has too many arguments starting with $this->aliasLoader.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
99
100
        $manager = new SiteAliasManager($this->aliasLoader);
101
        $result = $manager->get($aliasName);
102
        if (!$result) {
103
            throw new \Exception("No alias found");
104
        }
105
106
        return $result->export();
107
    }
108
109
    /**
110
     * Parse a site specification.
111
     *
112
     * @command site-spec:parse
113
     * @format yaml
114
     * @return array
115
     */
116
    public function parse($spec, $options = ['root' => ''])
117
    {
118
        $parser = new SiteSpecParser();
119
        return $parser->parse($spec, $options['root']);
120
    }
121
}
122