Issues (3)

lib/Configuration.php (1 issue)

1
<?php
2
namespace SimplePDO;
3
4
class Configuration
5
{
6
    /**
7
     * Configuration file.
8
     *
9
     * @var array
10
     */
11
    private $parsed_ini_file;
12
13
    /**
14
     * Constructor.
15
     *
16
     * @param  string $inifile configuration file (ini)
17
     * @return void
18
     *
19
     * @throws RuntimeException if ini file not found or readable
20
     * @throws RuntimeException if failed to parse ini file
21
     */
22
    public function __construct($inifile)
23
    {
24
        if (!is_readable($inifile))
25
        {
26
            throw new \RuntimeException(__METHOD__ . ": unable to parse {$inifile}; file doesn't exists or readable");
27
        }
28
        
29
        if (($this->parsed_ini_file = parse_ini_file($inifile, true)) === false)
0 ignored issues
show
Documentation Bug introduced by
It seems like parse_ini_file($inifile, true) can also be of type false. However, the property $parsed_ini_file is declared as type array. 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...
30
        {
31
            throw new \RuntimeException(__METHOD__ . ": failed to parse {$inifile}.");
32
        }
33
    }
34
    
35
    /**
36
     * Retrieve property from configuration file.
37
     *
38
     * @param  string $section  section header
39
     * @param  string $property property (key)
40
     * @return string
41
     *
42
     * @throws RuntimeException failed to retrieve configuration property from ini
43
     */
44
    final public function get($section, $property)
45
    {
46
        if (!isset($this->parsed_ini_file[$section][$property]))
47
        {
48
            throw new \RuntimeException(__METHOD__ . ": failed to retrieve the '{$property}' property within the '{$section}' section.");
49
        }
50
        return $this->parsed_ini_file[$section][$property];
51
    }
52
}
53