SiteAliasCommands   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 0
loc 135
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A siteList() 0 11 1
A siteLoad() 0 15 2
A getLocationsAndAliasName() 0 14 3
A renderAliases() 0 13 3
A siteGet() 0 15 2
A siteValue() 0 16 2
A parse() 0 5 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");
0 ignored issues
show
Deprecated Code introduced by
The method Robo\Common\IO::io() has been deprecated with message: Use a style injector instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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'");
0 ignored issues
show
Deprecated Code introduced by
The method Robo\Common\IO::io() has been deprecated with message: Use a style injector instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
63
                $aliasName = $arg;
64
            } else {
65
                $this->io()->note("Add search location: $arg");
0 ignored issues
show
Deprecated Code introduced by
The method Robo\Common\IO::io() has been deprecated with message: Use a style injector instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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
     * Show contents of a single site alias.
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
     * Access a value from a single alias.
111
     *
112
     * @command site:value
113
     * @format yaml
114
     * @return string
115
     */
116
    public function siteValue(array $varArgs)
117
    {
118
        $this->aliasLoader = new SiteAliasFileLoader();
119
        $ymlLoader = new YamlDataFileLoader();
120
        $this->aliasLoader->addLoader('yml', $ymlLoader);
121
        $key = array_pop($varArgs);
122
        $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...
123
124
        $manager = new SiteAliasManager($this->aliasLoader);
125
        $result = $manager->get($aliasName);
126
        if (!$result) {
127
            throw new \Exception("No alias found");
128
        }
129
130
        return $result->get($key);
131
    }
132
133
    /**
134
     * Parse a site specification.
135
     *
136
     * @command site-spec:parse
137
     * @format yaml
138
     * @return array
139
     */
140
    public function parse($spec, $options = ['root' => ''])
141
    {
142
        $parser = new SiteSpecParser();
143
        return $parser->parse($spec, $options['root']);
144
    }
145
}
146