Pipe   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 84
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 13 2
A open() 0 9 1
A write() 0 10 1
A read() 0 6 1
1
<?php
2
3
namespace PHPetroleum;
4
5
class Pipe
6
{
7
    private $name;
8
    private $pipe;
9
    private $size;
10
11
    /**
12
     * @param string $name
13
     * @param int $size
14
     *
15
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
16
     */
17
    public function __construct($name, $size = 8)
18
    {
19
        $this->name = $name;
20
        $this->size = $size;
21
    }
22
23
    /**
24
     * Create the file if needed
25
     *
26
     * @return Pipe
0 ignored issues
show
Documentation introduced by
Should the return type not be Pipe|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
27
     */
28
    public function create(){
29
        if (!is_file($this->name)) {
30
31
            return;
32
        }
33
34
        posix_mkfifo(
35
            $this->name,
36
            0777
37
        );
38
39
        return $this;
40
    }
41
42
    /**
43
     * Open the file with the given mode
44
     *
45
     * @param string $mode
46
     *
47
     * @return resource
48
     */
49
    public function open($mode)
50
    {
51
        $this->create();
52
53
        return $this->pipe = fopen(
54
            $this->name,
55
            $mode
56
        );
57
    }
58
59
    /**
60
     * Write the content into the file
61
     *
62
     *
63
     * @param string $string
64
     * @return Pipe
65
     */
66
    public function write($string){
67
        $string = (string)$string;
68
        $length = strlen($string);
69
70
        $this->open('w');
71
        fwrite($this->pipe, str_pad($length, $this->size, "0", STR_PAD_LEFT));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 0 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
72
        fwrite($this->pipe, $string);
73
74
        return $this;
75
    }
76
77
    /**
78
     * Wait for file content. Return the content
79
     *
80
     * @return string
81
     */
82
    public function read(){
83
        $this->open('r');
84
        $length = fread($this->pipe, $this->size);
85
86
        return fread($this->pipe, intval($length));
87
    }
88
}
89