Completed
Push — master ( dd0d81...a145e7 )
by Andrii
11:53
created

TravisYamlController::getTravis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Travis CI plugin for HiDev
5
 *
6
 * @link      https://github.com/hiqdev/hidev-travis
7
 * @package   hidev-travis
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\travis\controllers;
13
14
/**
15
 * Goal for .travis.yml config file.
16
 */
17
class TravisYamlController extends \hidev\controllers\FileController
18
{
19
    protected $_file = '.travis.yml';
20
21
    protected $_bin;
22
23
    public $sudo = false;
24
25
    public function getBin()
26
    {
27
        if ($this->_bin === null) {
28
            $this->_bin = $this->detectBin();
29
        }
30
31
        return $this->_bin;
32
    }
33
34
    public function detectBin()
35
    {
36
        if ($this->takePackage()->fullName === 'hiqdev/hidev') {
37
            return './bin/hidev';
38
        }
39
        if ($this->takePackage()->hasRequireAny('hiqdev/hidev')) {
40
            return './vendor/bin/hidev';
41
        }
42
43
        return './hidev.phar';
44
    }
45
46
    public function getBeforeInstall()
47
    {
48
        $commands = $this->get('before_install');
49
        if ($this->getItem('language') != 'php') {
50
            $this->sudo = true;
51
            $commands[] = 'sudo add-apt-repository --yes ppa:ondrej/php';
52
            $commands[] = 'sudo apt-get update';
53
            $commands[] = 'sudo apt-get install php5.6-cli php5.6-mbstring';
54
            $commands[] = 'env php -v';
55
        }
56
        if ($this->bin === './hidev.phar') {
0 ignored issues
show
Documentation introduced by
The property bin does not exist on object<hidev\travis\cont...s\TravisYamlController>. 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...
57
            $commands[] = 'wget http://hiqdev.com/hidev/hidev.phar -O hidev.phar && chmod a+x hidev.phar';
58
        } else {
59
            $commands[] = 'composer install --no-interaction';
60
        }
61
        $commands[] = $this->getBin() . ' --version';
62
        $commands[] = $this->getBin() . ' travis/before_install';
63
64
        return array_values(array_unique($commands));
65
    }
66
67
    /**
68
     * Reorders config elements.
69
     */
70
    public function actionSave()
71
    {
72
        $this->addActionItems();
73
        $this->prependLanguageOptions();
74
75
        return parent::actionSave();
76
    }
77
78
    public function addActionItems()
79
    {
80
        $add_items = [
81
            'sudo'           => false,
82
            'before_install' => $this->getBeforeInstall(),
83
        ];
84
        foreach (['install', 'before_script', 'script', 'after_success', 'after_failure', 'after_script'] as $event) {
85
            if ($this->takeGoal('travis')->{$event}) {
86
                $add_items[$event] = [$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->takePackage()->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