File::log()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Balloon
7
 *
8
 * @author      Raffael Sahli <[email protected]>
9
 * @copyright   Copryright (c) 2012-2017 gyselroth GmbH (https://gyselroth.com)
10
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
11
 */
12
13
namespace Micro\Log\Adapter;
14
15
class File extends AbstractAdapter
16
{
17
    /**
18
     * Filename.
19
     *
20
     * @var string
21
     */
22
    protected $file = '/tmp/out.log';
23
24
    /**
25
     * Filename.
26
     *
27
     * @var resource
28
     */
29
    protected $resource;
30
31
    /**
32
     * Set options.
33
     *
34
     * @return AdapterInterface
35
     */
36
    public function setOptions(? Iterable $config = null): AdapterInterface
37
    {
38
        if (null === $config) {
39
            $this->resource = fopen($this->file, 'a');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($this->file, 'a') can also be of type false. However, the property $resource 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...
40
41
            return $this;
42
        }
43
44
        foreach ($config as $option => $value) {
45
            switch ($option) {
46
                case 'file':
47
                    $this->file = (string) $value;
48
                    unset($config[$option]);
49
50
                break;
51
            }
52
        }
53
54
        parent::setOptions($config);
55
56
        $this->resource = fopen($this->file, 'a');
57
58
        return $this;
59
    }
60
61
    /**
62
     * Log.
63
     *
64
     * @param int    $priority
65
     * @param string $message
66
     *
67
     * @return bool
68
     */
69
    public function log(string $priority, string $message): bool
70
    {
71
        if (!is_resource($this->resource)) {
72
            return false;
73
        }
74
75
        $result = fwrite($this->resource, $message."\n");
76
77
        return (bool) $result;
78
    }
79
}
80