Completed
Push — master ( ca7425...09dfab )
by Dane
05:18
created

Pasteboard::defaultHeartbeat()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Dlindberg\Pasteboard;
4
5
class Pasteboard
6
{
7
    private static $clipboard = "foo";
8
9 9
    public static function set($value)
10
    {
11 9
        return self::action('pbcopy', $value);
12
    }
13
14 9
    public static function get()
15
    {
16 9
        return self::action('pbpaste');
17
    }
18
19 5
    public static function setArray($array, $options = array())
20
    {
21 5
        $config = self::configureArray($options);
22 5
        self::storedClipboard('pbpaste', $config['reset']);
23 5
        foreach ($array as $value) {
24 5
            if (!self::setArrayValue($value, $config)) {
25 5
                return false;
26
            }
27
        }
28 5
        self::storedClipboard('pbcopy', $config['reset']);
29
30 5
        return true;
31
    }
32
33 9
    private static function action($action, $value = null)
34
    {
35 9
        $output = false;
36 9
        $do = proc_open(
37 9
            $action,
38
            array(
39 9
                0 => array("pipe", "r"),
40
                1 => array("pipe", "w"),
41
                2 => array("pipe", "w"),
42
            ),
43 9
            $pipes
44
        );
45 9
        if (is_resource($do)) {
46
            switch ($action) {
47 9
                case 'pbcopy':
48 9
                    if (isset($value)) {
49 9
                        fwrite($pipes[0], $value);
50
                    }
51 9
                    break;
52 9
                case 'pbpaste':
53 9
                    $output = stream_get_contents($pipes[1]);
54 9
                    break;
55
            }
56 9
            foreach ($pipes as $k => $v) {
57 9
                fclose($pipes[$k]);
58
            }
59 9
            $status = proc_close($do);
60
        }
61 9
        if (isset($status) && $status === 0 && $output === false) {
62 9
            $output = true;
63 9
        } elseif (isset($status) && $status === 0 && mb_strlen($output) === 0) {
64 9
            $output = false;
65
        }
66
67 9
        return $output;
68
    }
69
70 5
    private static function storedClipboard($do, $test = true)
71
    {
72 5
        if ($test) {
73 1
            self::$clipboard = self::action($do, self::$clipboard);
0 ignored issues
show
Documentation Bug introduced by
It seems like self::action($do, self::$clipboard) can also be of type boolean. However, the property $clipboard is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74
        }
75 5
    }
76
77 5
    private static function configureArray($options)
78
    {
79 5
        $config['reset'] = self::setOption('reset', false, $options);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
80 5
        $config['depth'] = self::setOption('depth', 0, $options);
81 5
        $config['wait'] = self::setOption('wait', 1, $options);
82 5
        $config['heartbeat'] = self::setOption('heartbeat', self::defaultHeartbeat($config['wait']), $options);
83
84 5
        return $config;
85
    }
86
87
    private static function defaultHeartbeat($wait)
88
    {
89 5
        return function ($result) use ($wait) {
90 5
            $return = false;
91 5
            if ($result) {
92 5
                sleep($wait);
93 5
                $return = true;
94
            }
95
96 5
            return $return;
97 5
        };
98
99
    }
100
101 5
    private static function setOption($name, $default, $requested)
102
    {
103 5
        if (isset($requested[$name])) {
104 4
            return $requested[$name];
105
        } else {
106 5
            return $default;
107
        }
108
    }
109
110 5
    private static function setArrayValue($value, $config)
111
    {
112 5
        if (!is_array($value)) {
113 5
            $return = $config['heartbeat'](self::set($value));
114 3
        } elseif ($config['depth'] != 0) {
115 3
            $return = self::setArray($value,
116 3
                array('depth' => $config['depth'] - 1, 'heartbeat' => $config['heartbeat'],));
117
        } else {
118 2
            $return = true;
119
        }
120
121 5
        return $return;
122
    }
123
}
124