Completed
Push — master ( cf3c15...ee406f )
by Alexander
03:07
created

StreamMetaData::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 11
cp 0.9091
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 5
nop 2
crap 4.0119
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument\Transformer;
12
13
use Go\Instrument\PathResolver;
14
use InvalidArgumentException;
15
16
/**
17
 * Stream metadata object
18
 */
19
class StreamMetaData
20
{
21
    /**
22
     * Mapping between array keys and properties
23
     *
24
     * @var array
25
     */
26
    private static $propertyMap = [
27
        'timed_out'    => 'isTimedOut',
28
        'blocked'      => 'isBlocked',
29
        'eof'          => 'isEOF',
30
        'unread_bytes' => 'unreadBytesCount',
31
        'stream_type'  => 'streamType',
32
        'wrapper_type' => 'wrapperType',
33
        'wrapper_data' => 'wrapperData',
34
        'filters'      => 'filterList',
35
        'mode'         => 'mode',
36
        'seekable'     => 'isSeekable',
37
        'uri'          => 'uri',
38
    ];
39
40
    /**
41
     * TRUE if the stream timed out while waiting for data on the last call to fread() or fgets().
42
     *
43
     * @var bool
44
     */
45
    public $isTimedOut;
46
47
    /**
48
     * TRUE if the stream has reached end-of-file.
49
     *
50
     * @var bool
51
     */
52
    public $isBlocked;
53
54
    /**
55
     * TRUE if the stream has reached end-of-file.
56
     *
57
     * @var bool
58
     */
59
    public $isEOF;
60
61
    /**
62
     * The number of bytes currently contained in the PHP's own internal buffer.
63
     *
64
     * @var integer
65
     */
66
    public $unreadBytesCount;
67
68
    /**
69
     * A label describing the underlying implementation of the stream.
70
     *
71
     * @var string
72
     */
73
    public $streamType;
74
75
    /**
76
     * A label describing the protocol wrapper implementation layered over the stream.
77
     *
78
     * @var string
79
     */
80
    public $wrapperType;
81
82
    /**
83
     * Wrapper-specific data attached to this stream.
84
     *
85
     * @var mixed
86
     */
87
    public $wrapperData;
88
89
    /**
90
     * Array containing the names of any filters that have been stacked onto this stream.
91
     *
92
     * @var array
93
     */
94
    public $filterList;
95
96
    /**
97
     * The type of access required for this stream
98
     *
99
     * @var string
100
     */
101
    public $mode;
102
103
    /**
104
     * Whether the current stream can be seeked.
105
     *
106
     * @var bool
107
     */
108
    public $isSeekable;
109
110
    /**
111
     * The URI/filename associated with this stream.
112
     *
113
     * @var string
114
     */
115
    public $uri;
116
117
    /**
118
     * The contents of the stream.
119
     *
120
     * @var string
121
     */
122
    public $source;
123
124
    /**
125
     * Creates metadata object from stream
126
     *
127
     * @param resource $stream Instance of stream
128
     * @param string $source Source code or null
129
     * @throws \InvalidArgumentException for invalid stream
130
     */
131 38
    public function __construct($stream, $source = null)
132
    {
133 38
        if (!is_resource($stream)) {
134
            throw new InvalidArgumentException("Stream should be valid resource");
135
        }
136 38
        $metadata     = stream_get_meta_data($stream);
137 38
        $this->source = $source;
138 38
        if (preg_match('/resource=(.+)$/', $metadata['uri'], $matches)) {
139 15
            $metadata['uri'] = PathResolver::realpath($matches[1]);
140
        }
141 38
        foreach ($metadata as $key=>$value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
142 38
            $mappedKey = self::$propertyMap[$key];
143 38
            $this->$mappedKey = $value;
144
        }
145 38
    }
146
}
147