AbstractStream::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * PHP SAPI Library
4
 * Copyright (C) 2020 Christian Neff
5
 *
6
 * Permission to use, copy, modify, and/or distribute this software for
7
 * any purpose with or without fee is hereby granted, provided that the
8
 * above copyright notice and this permission notice appear in all copies.
9
 */
10
11
namespace Secondtruth\SAPI\Stream;
12
13
/**
14
 * The AbstractStream class.
15
 *
16
 * @author Christian Neff <[email protected]>
17
 */
18
abstract class AbstractStream
19
{
20
    /**
21
     * @var resource
22
     */
23
    protected $stream;
24
25
    /**
26
     * Stream constructor.
27
     *
28
     * @param resource $stream
29
     */
30 2
    public function __construct($stream)
31
    {
32 2
        $this->setStream($stream);
33 2
    }
34
35
    /**
36
     * @return resource
37
     */
38
    public function getStream()
39
    {
40
        return $this->stream;
41
    }
42
43
    /**
44
     * @param resource|string $stream
45
     */
46 2
    protected function setStream($stream): void
47
    {
48 2
        if (is_string($stream)) {
49
            $stream = $this->openStream($stream);
50 2
        } elseif (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
51
            throw new \InvalidArgumentException('The stream must be a valid resource or a path string.');
52
        }
53
54 2
        $this->stream = $stream;
0 ignored issues
show
Documentation Bug introduced by
It seems like $stream can also be of type false. However, the property $stream is declared as type resource. 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...
55 2
    }
56
57
    /**
58
     * @param string $stream
59
     *
60
     * @return resource|false
61
     */
62
    abstract protected function openStream(string $stream);
63
}
64