TravisYaml   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBin() 0 8 2
A detectBin() 0 11 3
A getBeforeInstall() 0 20 3
A save() 0 7 1
A addActionItems() 0 14 3
A prependLanguageOptions() 0 12 2
1
<?php
2
/**
3
 * Travis CI plugin for HiDev.
4
 *
5
 * @link      https://github.com/hiqdev/hidev-travis
6
 * @package   hidev-travis
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\travis\components;
12
13
/**
14
 * `.travis.yml` config file.
15
 */
16
class TravisYaml extends \hidev\components\File
17
{
18
    protected $_file = '.travis.yml';
19
20
    protected $_bin;
21
22
    public $sudo = false;
23
24
    public function getBin()
25
    {
26
        if ($this->_bin === null) {
27
            $this->_bin = $this->detectBin();
28
        }
29
30
        return $this->_bin;
31
    }
32
33
    public function detectBin()
34
    {
35
        if ($this->take('package')->fullName === 'hiqdev/hidev') {
36
            return './bin/hidev';
37
        }
38
        if ($this->take('package')->hasRequireAny('hiqdev/hidev')) {
39
            return './vendor/bin/hidev';
40
        }
41
42
        return './hidev.phar';
43
    }
44
45
    public function getBeforeInstall()
46
    {
47
        $commands = $this->get('before_install');
48
        if ($this->getItem('language') !== 'php') {
49
            $this->sudo = true;
50
            $commands[] = 'sudo add-apt-repository --yes ppa:ondrej/php';
51
            $commands[] = 'sudo apt-get update';
52
            $commands[] = 'sudo apt-get install php5.6-cli php5.6-mbstring';
53
            $commands[] = 'env php -v';
54
        }
55
        if ($this->bin === './hidev.phar') {
0 ignored issues
show
Documentation introduced by
The property bin does not exist on object<hidev\travis\components\TravisYaml>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
            $commands[] = 'wget http://hiqdev.com/hidev/hidev.phar -O hidev.phar && chmod a+x hidev.phar';
57
        } else {
58
            $commands[] = 'composer install --no-interaction';
59
        }
60
        $commands[] = $this->getBin() . ' --version';
61
        $commands[] = $this->getBin() . ' travis/before-install';
62
63
        return array_values(array_unique($commands));
64
    }
65
66
    /**
67
     * Reorders config elements.
68
     */
69
    public function save()
70
    {
71
        $this->addActionItems();
72
        $this->prependLanguageOptions();
73
74
        return parent::save();
75
    }
76
77
    public function addActionItems()
78
    {
79
        $add_items = [
80
            'sudo'           => false,
81
            'before_install' => $this->getBeforeInstall(),
82
        ];
83
        foreach (['install', 'before-script', 'script', 'after-success', 'after-failure', 'after-script'] as $event) {
84
            $tname = strtr($event, '-', '_');
85
            if ($this->take('travis')->{$tname}) {
86
                $add_items[$tname] = [$this->getBin() . ' travis/' . $event];
87
            }
88
        }
89
        $this->setItems($add_items);
90
    }
91
92
    public function prependLanguageOptions()
93
    {
94
        $items = $this->_items;
95
        $language = $items['language'] ?: $this->take('package')->getLanguage();
96
        $lang_ops = $items[$language];
97
        unset($items['language'], $items[$language]);
98
        $this->_items = [
99
            'language' => $language,
100
            $language  => $lang_ops,
101
        ] + $items;
102
        $this->setItem('sudo', $this->sudo);
0 ignored issues
show
Documentation introduced by
$this->sudo is of type boolean, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
    }
104
}
105