Completed
Push — master ( 26ca47...7361f3 )
by Dane
04:56
created

Pasteboard::doAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 8.8571
cc 5
eloc 13
nc 5
nop 2
crap 5
1
<?php
2
3
namespace Dlindberg\Pasteboard;
4
5
class Pasteboard
6
{
7
    private static $clipboard = "foo";
8
    private static $process;
9
    private static $pipes;
10
11
    /**
12
     * @param $value
13
     * @return bool|string
14
     */
15 9
    public static function set($value)
16
    {
17 9
        return self::action('pbcopy', $value);
18
    }
19
20
    /**
21
     * @return bool|string
22
     */
23 9
    public static function get()
24
    {
25 9
        return self::action('pbpaste');
26
    }
27
28
    /**
29
     * @param       $array
30
     * @param array $options
31
     * @return bool
32
     */
33 5
    public static function setArray($array, $options = array())
34
    {
35 5
        $config = self::configureArray($options);
36 5
        self::storedClipboard('pbpaste', $config['reset']);
37 5
        foreach ($array as $value) {
38 5
            if (!self::setArrayValue($value, $config)) {
39 5
                return false;
40
            }
41
        }
42 5
        self::storedClipboard('pbcopy', $config['reset']);
43
44 5
        return true;
45
    }
46
47
    /**
48
     * @param      $action
49
     * @param null $value
50
     * @return bool|string
51
     */
52 9
    private static function action($action, $value = null)
53
    {
54 9
        $output = false;
55 9
        if (self::openProcess($action)) {
56 9
            $output = self::doAction($action, $value);
57 9
            self::closeProcess();
58
        }
59
60 9
        return $output;
61
    }
62
63
    /**
64
     * @param $process
65
     * @return bool
66
     */
67 9
    private static function openProcess($process)
68
    {
69 9
        $return = false;
70 9
        self::$process = proc_open(
71 9
            $process,
72
            array(
73 9
                0 => array("pipe", "r"),
74
                1 => array("pipe", "w"),
75
                2 => array("pipe", "w"),
76
            ),
77 9
            self::$pipes
78
        );
79 9
        if (is_resource(self::$process)) {
80 9
            $return = true;
81
        }
82
83 9
        return $return;
84
    }
85
86
    /**
87
     * @return int
88
     */
89 9
    private static function closeProcess()
90
    {
91 9
        foreach (self::$pipes as $k => $v) {
92 9
            fclose(self::$pipes[$k]);
93
        }
94
95 9
        return proc_close(self::$process);
96
    }
97
98
    /**
99
     * @param $action
100
     * @param $value
101
     * @return bool|string
102
     */
103 9
    private static function doAction($action, $value)
104
    {
105 9
        $output = true;
106
        switch ($action) {
107 9
            case 'pbcopy':
108 9
                if (isset($value)) {
109 9
                    fwrite(self::$pipes[0], $value);
110
                }
111 9
                break;
112 9
            case 'pbpaste':
113 9
                $output = stream_get_contents(self::$pipes[1]);
114 9
                if (mb_strlen($output) < 1) {
115 9
                    $output = false;
116
                }
117 9
                break;
118
        }
119
120 9
        return $output;
121
    }
122
123
    /**
124
     * @param      $do
125
     * @param bool $test
126
     */
127 5
    private static function storedClipboard($do, $test = true)
128
    {
129 5
        if ($test) {
130 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...
131
        }
132 5
    }
133
134
    /**
135
     * @param $options
136
     * @return mixed
137
     */
138 5
    private static function configureArray($options)
139
    {
140 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...
141 5
        $config['depth'] = self::setOption('depth', 0, $options);
142 5
        $config['wait'] = self::setOption('wait', 1, $options);
143 5
        $config['heartbeat'] = self::setOption('heartbeat', self::defaultHeartbeat($config['wait']), $options);
144
145 5
        return $config;
146
    }
147
148
    /**
149
     * @param $wait
150
     * @return \Closure
151
     */
152
    private static function defaultHeartbeat($wait)
153
    {
154 5
        return function ($result) use ($wait) {
155 5
            $return = false;
156 5
            if ($result) {
157 5
                sleep($wait);
158 5
                $return = true;
159
            }
160
161 5
            return $return;
162 5
        };
163
    }
164
165
    /**
166
     * @param $name
167
     * @param $default
168
     * @param $requested
169
     * @return mixed
170
     */
171 5
    private static function setOption($name, $default, $requested)
172
    {
173 5
        if (isset($requested[$name])) {
174 4
            return $requested[$name];
175
        } else {
176 5
            return $default;
177
        }
178
    }
179
180
    /**
181
     * @param $value
182
     * @param $config
183
     * @return bool
184
     */
185 5
    private static function setArrayValue($value, $config)
186
    {
187 5
        if (!is_array($value)) {
188 5
            $return = $config['heartbeat'](self::set($value));
189 3
        } elseif ($config['depth'] != 0) {
190 3
            $return = self::setArray(
191 3
                $value,
192 3
                array('depth' => $config['depth'] - 1, 'heartbeat' => $config['heartbeat'],)
193
            );
194
        } else {
195 2
            $return = true;
196
        }
197
198 5
        return $return;
199
    }
200
}
201