Completed
Push — master ( c5f7b8...442191 )
by Roberto
03:20
created

PhpSerial::setPort()   C

Complexity

Conditions 10
Paths 11

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 17.467

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 11
cts 19
cp 0.5789
rs 6.1368
cc 10
eloc 17
nc 11
nop 1
crap 17.467

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Posprint\Extras;
4
5
/**
6
 * Serial port control class
7
 *
8
 * Refactoring from original, https://github.com/Xowap/PHP-Serial, to meet PSR standards
9
 * and propose improvements and fixes to the fact that the original is not actively
10
 * maintained for many years.
11
 * by Roberto L. Machado <linux dot rlm at gmail dot com>
12
 * 
13
 * IMPORTANT: check and adjust permissions for serial port access by server user like www-data
14
 * 
15
 * @author Rémy Sanchez <[email protected]>
16
 * @author Rizwan Kassim <[email protected]>
17
 * @thanks Aurélien Derouineau for finding how to open serial ports with windows
18
 * @thanks Alec Avedisyan for help and testing with reading
19
 * @thanks Jim Wright for OSX cleanup/fixes.
20
 * @copyright under GPL 2 licence
21
 */
22
23
use RuntimeException;
24
use InvalidArgumentException;
25
26
class PhpSerial
27
{
28
    const OS_UNKNOWN = 0;
29
    const OS_WIN = 1; //WINS32 WINNT Windows
30
    const OS_LINUX = 2;
31
    const OS_CYGWIN = 3; //Cygwin Windows Linux like commands
32
    const OS_UNIX = 4;
33
    const OS_BSD = 5; //FreeBSD or NetBSD or OpenBSD /dev/ttyu1
34
    const OS_OSX = 6; //Darwin MacOS
35
    const OS_HPUX = 7; //tty1p0
36
    const SERIAL_DEVICE_NOTSET = 0;
37
    const SERIAL_DEVICE_SET = 1;
38
    const SERIAL_DEVICE_OPENED = 2;
39
    const PARITY_NONE = 0;
40
    const PARITY_ODD = 1;
41
    const PARITY_EVEN = 2;
42
    const FLOW_NONE = 0; //no flow control
43
    const FLOW_RTSCTS = 1; // use RTS/CTS handshaking
44
    const FLOW_XONXOFF = 2; //use XON/XOFF protocol
45
    
46
    /**
47
     * Pointer for device
48
     * 
49
     * @var resource
50
     */
51
    protected $handle = null;
52
    /**
53
     * Data buffer
54
     * 
55
     * @var string
56
     */
57
    protected $buffer = "";
58
    /**
59
     * This var says if buffer should be flushed by write (true) or
60
     * manually (false)
61
     *
62
     * @var bool
63
     */
64
    protected $autoflush = false;
65
    /**
66
     * Wait time after send data to serial
67
     * 
68
     * @var float
69
     */
70
    protected $waittime = 0.1;
71
    /**
72
     * OS type where php is running
73
     * 
74
     * @var int
75
     */
76
    protected $ostype = 2; //linux is default
77
    /**
78
     * Mode command to set up serial port
79
     * 
80
     * @var string
81
     */
82
    protected $mode = ''; //formated device mode for especific OS use
83
    /**
84
     * Status of port
85
     * NoSet, Set or Open
86
     * 
87
     * @var int
88
     */
89
    protected $state = self::SERIAL_DEVICE_NOTSET;
90
    /**
91
     * Port name
92
     * 
93
     * @var string
94
     */
95
    protected $port = '/dev/ttyS0';
96
    /**
97
     * Data bits
98
     * 
99
     * @var int
100
     */
101
    protected $databits = 8;
102
    /**
103
     * Baud Rate
104
     * 
105
     * @var int 
106
     */
107
    protected $baudrate = 9600;
108
    /**
109
     * Parity
110
     * 
111
     * @var int
112
     */
113
    protected $parity = self::PARITY_NONE;
114
    /**
115
     * Stop Bits
116
     * 
117
     * @var float
118
     */
119
    protected $stopbits = 1;
120
    /**
121
     * Flow Control
122
     * 
123
     * @var int
124
     */
125
    protected $flowcontrol = self::FLOW_NONE;
126
    /**
127
     * Formated device name command
128
     * 
129
     * @var string
130
     */
131
    protected $device = '/dev/ttyS0';
132
    /**
133
     * Formated Data Bits command
134
     * 
135
     * @var string
136
     */
137
    protected $formatedDataBits = 'cs8';
138
    /**
139
     * Formated Baud Rate command
140
     * 
141
     * @var string
142
     */
143
    protected $formatedBaudRate = '9600';
144
    /**
145
     * Formated parity command
146
     * 
147
     * @var string
148
     */
149
    protected $formatedParity = '-parenb';
150
    /**
151
     * Formated stop bits command
152
     * 
153
     * @var string
154
     */
155
    protected $formatedStopBits = '-cstopb';
156
    /**
157
     * Formated flow control command
158
     * 
159
     * @var string
160
     */
161
    protected $formatedFlowControl = 'clocal -crtscts -ixon -ixoff';
162
    
163
    /**
164
     * Parity data
165
     * 
166
     * @var array
167
     */
168
    private $parityargs = [
169
        "none" => [0, "-parenb"],
170
        "odd"  => [1, "parenb parodd"],
171
        "even" => [2, "parenb -parodd"]
172
    ];
173
    
174
    /**
175
     * Basud Rate data
176
     * 
177
     * @var array
178
     */
179
    private $baudsargs = array (
180
        110    => 11,
181
        150    => 15,
182
        300    => 30,
183
        600    => 60,
184
        1200   => 12,
185
        2400   => 24,
186
        4800   => 48,
187
        9600   => 96,
188
        19200  => 19,
189
        38400  => 38400,
190
        57600  => 57600,
191
        115200 => 115200
192
    );
193
194
    /**
195
     * Constructor
196
     * Set ostype parameter
197
     * 
198
     * @param int $forceOS
199
     */
200 14
    public function __construct($forceOS = null)
201
    {
202 14
        if (! is_null($forceOS)) {
203
            if ($this->ostype !== $forceOS && ($forceOS > 0 && $forceOS < 8)) {
204
                $this->ostype = $forceOS;
205
                //clear params
206
                $this->clearParams();
207
            }
208
        } else {
209 14
            $this->ostype = $this->getOs();
210
        }
211 14
    }
212
    
213
    /**
214
     * Clear class params
215
     * Used for testing proporses
216
     */
217
    protected function clearParams()
218
    {
219
        $this->mode = null;
220
        $this->state = null;
221
        $this->port = null;
222
        $this->databits = null;
223
        $this->baudrate = null;
224
        $this->parity = null;
225
        $this->stopbits = null;
226
        $this->flowcontrol = null;
227
        $this->device = null;
228
        $this->formatedDataBits = null;
229
        $this->formatedBaudRate = null;
230
        $this->formatedParity = null;
231
        $this->formatedStopBits = null;
232
        $this->formatedFlowControl = null;
233
    }
234
235
    /**
236
     * Close port
237
     */
238 14
    public function __destruct()
239
    {
240 14
        $this->close();
241 14
    }
242
    
243
    /**
244
     * Open set port
245
     * 
246
     * @return boolean
247
     */
248 2
    public function open()
249
    {
250 2
        if ($this->state === self::SERIAL_DEVICE_OPENED && is_resource($this->handle)) {
251
            return true;
252
        }
253
        //before the serial port is opened it must be configured,
254
        //and in windows environment, all sets at a single time
255 2
        $this->config();
0 ignored issues
show
Unused Code introduced by
The call to the method Posprint\Extras\PhpSerial::config() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
256 2
        $this->handle = @fopen($this->device, 'r+b');
257 2
        if ($this->handle === false) {
258 1
            $this->handle = null;
259 1
            $this->state = self::SERIAL_DEVICE_NOTSET;
260 1
            throw new RuntimeException('Fail to open device. Check permissions.');
261
        }
262 1
        stream_set_blocking($this->handle, false);
263 1
        $this->state = self::SERIAL_DEVICE_OPENED;
264 1
        return true;
265
    }
266
    
267
    /**
268
     * Close serial port
269
     * 
270
     * @return boolean
271
     */
272 14
    public function close()
273
    {
274 14
        if ($this->state !== self::SERIAL_DEVICE_OPENED || ! is_resource($this->handle)) {
275 13
            return true;
276
        }
277 1
        if (fclose($this->handle)) {
278 1
            $this->handle = null;
279 1
            $this->state = self::SERIAL_DEVICE_SET;
280 1
            return true;
281
        }
282
        return false;
283
    }
284
    
285
    /**
286
     * Use class parameters to configure the serial port
287
     */
288 2
    public function config()
289
    {
290 2
        if ($this->state === self::SERIAL_DEVICE_SET) {
291
            return true;
292
        }
293 2
        if ($this->ostype == 0) {
294
            return false;
295
        }
296
        $modesos = [
297 2
            1 => 'MODE', //windos mode com4: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off
298 2
            2 => "stty -F", //linux
299 2
            3 => "stty -F", //cygwin
300 2
            4 => 'stty -F', //unix
301 2
            5 => 'stty -F', //BSD
302 2
            6 => "stty -f", //MacOS
303
            7 => 'stty -F' //HPUX
304 2
        ];
305
306 2
        $mode = $modesos[$this->ostype]
307
                . " "
308 2
                . "$this->device "
309 2
                . "$this->formatedBaudRate "
310 2
                . "$this->formatedParity "
311 2
                . "$this->formatedDataBits "
312 2
                . "$this->formatedStopBits "
313 2
                . "$this->formatedFlowControl";
314
        
315 2
        return $mode;
316
    }
317
    
318
    /**
319
     * Set automatic send massage to serial
320
     * 
321
     * @param bool $auto
322
     * @param float $time
0 ignored issues
show
Bug introduced by
There is no parameter named $time. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
323
     */
324
    public function setAuto($auto, $waittime)
325
    {
326
        if (! is_bool($auto)) {
327
            $data = false;
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
328
        }
329
        if (! is_float($waittime)) {
330
            $time = 0.1;
0 ignored issues
show
Unused Code introduced by
$time is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
331
        }
332
        $this->waittime = $waittime;
333
        $this->autoflush = $auto;
334
    }
335
    
336
    /**
337
     * Returns automatic mode
338
     * 
339
     * @return bool
340
     */
341
    public function getAuto()
342
    {
343
        return $this->autoflush;
344
    }
345
346
    /**
347
     * Read serial port
348
     * 
349
     * @return string
350
     */
351
    public function read()
352
    {
353
        return '';
354
    }
355
    
356
    /**
357
     * Write data to buffer or serial port
358
     * depends of getAuto()
359
     * if  getAuto() == true this command writes directly to port
360
     * if  getAuto() == false this command writes to buffer (default)
361
     * 
362
     * @param string $data
363
     * @return boolean
364
     */
365
    public function write($data)
366
    {
367
        $this->buffer .= $data;
368
        if ($this->autoflush === true) {
369
            $this->flush();
370
            usleep((int) ($this->waittime * 1000000));
371
        }
372
        return true;
373
    }
374
    
375
    /**
376
     * Flushs imediatly data to serial port
377
     * 
378
     * @return boolean
379
     */
380
    public function flush()
381
    {
382
        if (fwrite($this->handle, $this->buffer) !== false) {
383
            $this->buffer = "";
384
            return true;
385
        }
386
        return false;
387
    }
388
389
    /**
390
     * Set port name
391
     * 
392
     * @param string $port
393
     */
394 1
    public function setPort($port)
395
    {
396
        //identify input if $port like COM?? even in others OS
397 1
        $flagWinMode = preg_match("@^COM(\d+):?$@i", $port, $matches);
398
        //select port from OS type
399 1
        switch ($this->ostype) {
400 1
            case self::OS_WIN:
401
                $this->device = ($flagWinMode) ? "COM$matches[1]:" : $port;
402
                break;
403 1
            case self::OS_LINUX:
404 1
            case self::OS_CYGWIN:
405 1
                $this->device = ($flagWinMode) ? "/dev/ttyS".($matches[1]-1) : $port;
406 1
                break;
407
            case self::OS_UNIX:
408
            case self::OS_BSD:
409
            case self::OS_OSX:
410
            case self::OS_HPUX:
411
            default:
412
                $this->device = $port;
413 1
        }
414 1
        $this->port = $port;
415 1
    }
416
    
417
    /**
418
     * Returns port name
419
     *  
420
     * @return string
421
     */
422 1
    public function getPort()
423
    {
424 1
        return $this->port;
425
    }
426
    
427
    /**
428
     * Returns device formated name
429
     * 
430
     * @return string
431
     */
432 1
    public function getDevice()
433
    {
434 1
        return $this->device;
435
    }
436
    
437
    /**
438
     * Sets the length of a character.
439
     * length of a character (5 <= length <= 8)
440
     * 
441
     * @param int $length
442
     * @return boolean
443
     */
444 1
    public function setDataBits($length)
445
    {
446 1
        if ($length < 5 || $length > 8) {
447
            $length = 8;
448
        }
449 1
        $this->databits = $length;
450 1
        $this->formatedDataBits = $this->zDataBits($length);
451 1
        return true;
452
    }
453
    
454
    /**
455
     * Returns char length
456
     * 
457
     * @return int
458
     */
459 2
    public function getDataBits()
460
    {
461 2
        return $this->databits;
462
    }
463
    
464
    /**
465
     * Format data bits commands
466
     * 
467
     * @param int $length
468
     * @return string
469
     */
470 1
    protected function zDataBits($length)
471
    {
472 1
        $fdatabits = "cs$length";
473 1
        if ($this->ostype == self::OS_WIN) {
474
            //windows
475
            $fdatabits = "DATA=$length";
476
        }
477 1
        return $fdatabits;
478
    }
479
480
    /**
481
     * Set serial baud rate
482
     * 
483
     * @param int $rate
484
     * @return boolean
485
     */
486 1
    public function setBaudRate($rate)
487
    {
488 1
        if (! isset($this->baudsargs[$rate])) {
489
            $rate = 9600;
490
        }
491 1
        $this->baudrate = $rate;
492 1
        $this->formatedBaudRate = $this->zBaudRate($rate);
493 1
        return true;
494
    }
495
    
496
    /**
497
     * Return baud rate
498
     * 
499
     * @return int
500
     */
501 2
    public function getBaudRate()
502
    {
503 2
        return $this->baudrate;
504
    }
505
    
506
    /**
507
     * Format baud rate command
508
     * 
509
     * @param int $rate
510
     * @return string
511
     */
512 1
    protected function zBaudRate($rate)
513
    {
514 1
        $baud = "$rate";
515 1
        if ($this->ostype == self::OS_WIN) {
516
            //windows
517
            $baud = "BAUD=".$this->baudsargs[$rate];
518
        }
519 1
        return $baud;
520
    }
521
522
523
    /**
524
     * Sets parity mode
525
     * 
526
     * @param string $parity odd, even, none
527
     * @return boolean
528
     */
529 1
    public function setParity($parity)
530
    {
531 1
        if (! isset($this->parityargs[$parity])) {
532
            $parity = 'none';
533
        }
534 1
        $this->parity = $this->parityargs[$parity][0];
535 1
        $this->formatedParity = $this->zParity($parity);
536 1
        return true;
537
    }
538
    
539
    /**
540
     * Get parity mode set
541
     * 
542
     * @return string
543
     */
544 2
    public function getParity()
545
    {
546 2
        switch ($this->parity) {
547 2
            case 0:
548 1
                return 'none';
549 1
            case 1:
550 1
                return 'odd';
551
            case 2:
552
                return 'even';
553
        }
554
    }
555
    
556
    /**
557
     * Format parity command
558
     * 
559
     * @param string $parity
560
     * @return string
561
     */
562 1
    protected function zParity($parity)
563
    {
564 1
        $fparity = $this->parityargs[$parity][1];
565 1
        if ($this->ostype == self::OS_WIN) {
566
            //windows
567
            $fparity = "PARITY=" .  strtoupper(substr($parity, 0, 1));
568
        }
569 1
        return $fparity;
570
    }
571
572
573
    /**
574
     * Set length of stop bits
575
     * the length of a stop bit.
576
     * It must be either 1, 1.5 or 2.
577
     * 1.5 is not supported under linux and on some computers.
578
     * 
579
     * @param float $length
580
     * @return boolean
581
     */
582 1
    public function setStopBits($length)
583
    {
584 1
        if ($length !== 1 && $length !== 1.5 && $length !== 2) {
585
            $length = 1;
586
        }
587 1
        $this->stopbits = $length;
0 ignored issues
show
Documentation Bug introduced by
It seems like $length can also be of type integer. However, the property $stopbits is declared as type double. 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...
588 1
        $this->formatedStopBits = $this->zStopBits($length);
589 1
        return true;
590
    }
591
    
592
    /**
593
     * Return stop bits set
594
     * 
595
     * @return float
596
     */
597 2
    public function getStopBits()
598
    {
599 2
        return $this->stopbits;
600
    }
601
    
602
    /**
603
     * Format stop bit command
604
     * 
605
     * @param float $length
606
     * @return string
607
     */
608 1
    public function zStopBits($length)
609
    {
610 1
        $stopb = (($length == 1) ? "-" : "") . "cstopb";
611 1
        if ($this->ostype === self::OS_WIN) {
612
            $stopb = "STOP=" . $length;
613
        }
614 1
        return $stopb;
615
    }
616
    
617
    /**
618
     * Set the flow control mode.
619
     * Availible modes :
620
     *   "none" : no flow control
621
     *   "rts/cts" : use RTS/CTS handshaking
622
     *   "xon/xoff" : use XON/XOFF protocol
623
     * 
624
     * @param string $flow
625
     * @return boolean
626
     */
627 1
    public function setFlowControl($flow)
628
    {
629
        switch ($flow) {
630 1
            case 'rts/cts':
631
                $this->flowcontrol = self::FLOW_RTSCTS;
632
                break;
633 1
            case 'xon/xoff':
634 1
                $this->flowcontrol = self::FLOW_XONXOFF;
635 1
                break;
636
            default:
637
                $this->flowcontrol = self::FLOW_NONE;
638
        }
639 1
        $this->formatedFlowControl = $this->zFlowControl($this->flowcontrol);
640 1
        return true;
641
    }
642
    
643
    /**
644
     * Returns flow control set
645
     * 
646
     * @return string
647
     */
648 2
    public function getFlowControl()
649
    {
650 2
        switch ($this->flowcontrol) {
651 2
            case 0:
652 1
                return 'none';
653 1
            case 1:
654
                return 'rts/cts';
655 1
            case 2:
656 1
                return 'xon/xoff';
657
        }
658
    }
659
    
660
    /**
661
     * Return flow control command formated for OP type
662
     * 
663
     * @param int $flow
664
     * @return string
665
     */
666 1
    protected function zFlowControl($flow)
667
    {
668
        $modeos = [
669
            //windows
670 1
            self::OS_WIN => ["xon=off octs=off rts=on","xon=off octs=on rts=hs","xon=on octs=off rts=on"],
671
            //linux
672 1
            self::OS_LINUX => ["clocal -crtscts -ixon -ixoff","-clocal crtscts -ixon -ixoff","-clocal -crtscts ixon ixoff"],
673
            //cygwin
674 1
            self::OS_CYGWIN => ["clocal -crtscts -ixon -ixoff","-clocal crtscts -ixon -ixoff","-clocal -crtscts ixon ixoff"],
675
            //unix
676 1
            self::OS_UNIX => ["clocal -crtscts -ixon -ixoff","-clocal crtscts -ixon -ixoff","-clocal -crtscts ixon ixoff"],
677
            //bsd
678 1
            self::OS_BSD => ["clocal -crtscts -ixon -ixoff","-clocal crtscts -ixon -ixoff","-clocal -crtscts ixon ixoff"],
679
            //macos
680 1
            self::OS_OSX => ["clocal -crtscts -ixon -ixoff","-clocal crtscts -ixon -ixoff","-clocal -crtscts ixon ixoff"],
681
            //hpux
682 1
            self::OS_HPUX => ["clocal -crtscts -ixon -ixoff","-clocal crtscts -ixon -ixoff","-clocal -crtscts ixon ixoff"]
683 1
        ];
684 1
        return (string) $modeos[$this->ostype][$flow];
685
    }
686
    
687
    /**
688
     * Find OS type
689
     * 
690
     * @return int
691
     */
692 14
    protected function getOs()
693
    {
694 14
        switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'DAR') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'WIN') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'LINUX') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'CYGWIN') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'HPUX') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'BSD') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'UNIX') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
695 14
            case stristr(PHP_OS, 'DAR'):
696
                return self::OS_OSX;
697 14
            case stristr(PHP_OS, 'WIN'):
698
                return self::OS_WIN;
699 14
            case stristr(PHP_OS, 'LINUX'):
700 14
                return self::OS_LINUX;
701
            case stristr(PHP_OS, 'CYGWIN'):
702
                return self::OS_CYGWIN;
703
            case stristr(PHP_OS, 'HPUX'):
704
                return self::OS_HPUX;
705
            case stristr(PHP_OS, 'BSD'):
706
                return self::OS_BSD;
707
            case stristr(PHP_OS, 'UNIX'):
708
                return self::OS_UNIX;
709
            default:
710
                return self::OS_UNKNOWN;
711
        }
712
    }
713
    
714
    /**
715
     * Exec command line in OS console
716
     * 
717
     * @param type $cmd
718
     * @param type $out
719
     * @return type
720
     */
721
    protected function execCommand($cmd, &$out = null)
722
    {
723
        $desc = array(
724
            1 => array("pipe", "w"),
725
            2 => array("pipe", "w")
726
        );
727
        $proc = proc_open($cmd, $desc, $pipes);
728
        $ret = stream_get_contents($pipes[1]);
729
        $err = stream_get_contents($pipes[2]);
730
        fclose($pipes[1]);
731
        fclose($pipes[2]);
732
        $retVal = proc_close($proc);
733
        if (func_num_args() == 2) {
734
            $out = array($ret, $err);
735
        }
736
        return $retVal;
737
    }
738
}
739