Completed
Branch feature/goaop-parser (08838c)
by Alexander
03:32
created

StreamMetaData   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 90%
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 24
ccs 9
cts 10
cp 0.9
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
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 ArrayObject;
14
use Go\Instrument\PathResolver;
15
use InvalidArgumentException;
16
17
/**
18
 * Stream metadata object
19
 *
20
 * @property bool timed_out TRUE if the stream timed out while waiting for data on the last call to fread() or fgets().
21
 * @property bool blocked TRUE if the stream is in blocking IO mode
22
 * @property bool eof TRUE if the stream has reached end-of-file.
23
 * @property int unread_bytes the number of bytes currently contained in the PHP's own internal buffer.
24
 * @property string stream_type a label describing the underlying implementation of the stream.
25
 * @property string wrapper_type a label describing the protocol wrapper implementation layered over the stream.
26
 * @property mixed wrapper_data wrapper specific data attached to this stream.
27
 * @property array filters array containing the names of any filters that have been stacked onto this stream.
28
 * @property string mode the type of access required for this stream
29
 * @property bool seekable whether the current stream can be seeked.
30
 * @property string uri the URI/filename associated with this stream.
31
 * @property string source of the stream.
32
 */
33
class StreamMetaData extends ArrayObject
34
{
35
    /**
36
     * Creates metadata object from stream
37
     *
38
     * @param resource $stream Instance of stream
39
     * @param string $source Source code or null
40
     * @throws \InvalidArgumentException for invalid stream
41
     */
42 37
    public function __construct($stream, $source = null)
43
    {
44 37
        if (!is_resource($stream)) {
45
            throw new InvalidArgumentException("Stream should be valid resource");
46
        }
47 37
        $metadata = stream_get_meta_data($stream);
48 37
        if ($source) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $source of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
49 8
            $metadata['source'] = $source;
50
        }
51 37
        if (preg_match('/resource=(.+)$/', $metadata['uri'], $matches)) {
52 14
            $metadata['uri'] = PathResolver::realpath($matches[1]);
53
        }
54 37
        parent::__construct($metadata, ArrayObject::ARRAY_AS_PROPS);
55 37
    }
56
}
57