Issues (83)

src/Log/File.php (1 issue)

1
<?php
2
namespace phpbu\App\Log;
3
4
use InvalidArgumentException;
5
6
/**
7
 * Class that can write to a file or a socket.
8
 *
9
 *
10
 * @package    phpbu
11
 * @subpackage Log
12
 * @author     Sebastian Feldmann <[email protected]>
13
 * @copyright  Sebastian Feldmann <[email protected]>
14
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
15
 * @link       https://phpbu.de/
16
 * @since      Class available since Release 1.0.0
17
 */
18
class File
19
{
20
    /**
21
     * @var resource
22
     */
23
    protected $out;
24
25
    /**
26
     * @var string
27
     */
28
    protected $outTarget;
29
30
    /**
31
     * Set output target.
32
     *
33
     * @param mixed $out
34
     */
35 6
    public function setOut($out)
36
    {
37 6
        if (empty($out)) {
38 1
            throw new InvalidArgumentException('Out can\'t be empty');
39
        }
40 5
        if (is_string($out)) {
41 4
            $this->setupOut($out);
42
        } else {
43 1
            $this->out = $out;
44
        }
45 5
    }
46
47
    /**
48
     * Setup the out resource
49
     *
50
     * @param string $out
51
     */
52 4
    protected function setupOut(string $out)
53
    {
54 4
        if (strpos($out, 'php://') === false && !is_dir(dirname($out))) {
55 1
            mkdir(dirname($out), 0777, true);
56
        }
57 4
        $this->out       = fopen($out, 'wt');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($out, 'wt') can also be of type false. However, the property $out 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...
58 4
        $this->outTarget = $out;
59 4
    }
60
61
    /**
62
     * @param string $buffer
63
     */
64 5
    public function write($buffer)
65
    {
66 5
        fwrite($this->out, $buffer);
67 5
    }
68
69
    /**
70
     * Close output if it's not to a php stream.
71
     */
72 3
    public function close()
73
    {
74 3
        if ($this->out && strncmp($this->outTarget, 'php://', 6) !== 0) {
75 1
            fclose($this->out);
76
        }
77 3
    }
78
}
79