Completed
Push — master ( 7361f3...83291b )
by Dane
03:58
created

Pasteboard::doAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 12
nc 4
nop 2
crap 4
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
                    fwrite(self::$pipes[0], $value);
109 9
                break;
110 9
            case 'pbpaste':
111 9
                $output = stream_get_contents(self::$pipes[1]);
112 9
                if (mb_strlen($output) < 1) {
113 9
                    $output = false;
114
                }
115 9
                break;
116
        }
117
118 9
        return $output;
119
    }
120
121
    /**
122
     * @param string $do
123
     * @param bool   $test
124
     */
125 5
    private static function storedClipboard($do, $test = true)
126
    {
127 5
        if ($test) {
128 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...
129
        }
130 5
    }
131
132
    /**
133
     * @param $options
134
     * @return mixed
135
     */
136 5
    private static function configureArray($options)
137
    {
138 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...
139 5
        $config['depth'] = self::setOption('depth', 0, $options);
140 5
        $config['wait'] = self::setOption('wait', 1, $options);
141 5
        $config['heartbeat'] = self::setOption('heartbeat', self::defaultHeartbeat($config['wait']), $options);
142
143 5
        return $config;
144
    }
145
146
    /**
147
     * @param $wait
148
     * @return \Closure
149
     */
150
    private static function defaultHeartbeat($wait)
151
    {
152 5
        return function ($result) use ($wait) {
153 5
            $return = false;
154 5
            if ($result) {
155 5
                sleep($wait);
156 5
                $return = true;
157
            }
158
159 5
            return $return;
160 5
        };
161
    }
162
163
    /**
164
     * @param string $name
165
     * @param        $default
166
     * @param        $requested
167
     * @return mixed
168
     */
169 5
    private static function setOption($name, $default, $requested)
170
    {
171 5
        if (isset($requested[$name])) {
172 4
            return $requested[$name];
173
        } else {
174 5
            return $default;
175
        }
176
    }
177
178
    /**
179
     * @param $value
180
     * @param $config
181
     * @return bool
182
     */
183 5
    private static function setArrayValue($value, $config)
184
    {
185 5
        if (!is_array($value)) {
186 5
            $return = $config['heartbeat'](self::set($value));
187 3
        } elseif ($config['depth'] != 0) {
188 3
            $return = self::setArray(
189 3
                $value,
190 3
                array('depth' => $config['depth'] - 1, 'heartbeat' => $config['heartbeat'],)
191
            );
192
        } else {
193 2
            $return = true;
194
        }
195
196 5
        return $return;
197
    }
198
}
199