Issues (224)

requirement-checker/src/RequirementCollection.php (6 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\RequirementChecker;
16
17
use ArrayIterator;
18
use Countable;
19
use IteratorAggregate;
20
use Traversable;
21
use function count;
22
use function get_cfg_var;
23
24
/**
25
 * @private
26
 *
27
 * @see https://github.com/symfony/requirements-checker/blob/master/src/RequirementCollection.php
28
 *
29
 * @license MIT (c) Fabien Potencier <[email protected]>
30
 */
31
final class RequirementCollection implements IteratorAggregate, Countable
32
{
33
    /**
34
     * @var list<Requirement>
0 ignored issues
show
The type KevinGH\RequirementChecker\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
     */
36
    private $requirements = [];
37
38
    /**
39
     * @var string|false
40
     */
41
    private $phpIniPath;
42
43
    /**
44
     * @param string|false|null $phpIniPath
45
     */
46
    public function __construct($phpIniPath = null)
47
    {
48
        $this->phpIniPath = $phpIniPath ?? get_cfg_var('cfg_file_path');
0 ignored issues
show
Documentation Bug introduced by
It seems like $phpIniPath ?? get_cfg_var('cfg_file_path') can also be of type array. However, the property $phpIniPath is declared as type false|string. 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...
49
    }
50
51
    /**
52
     * @return Traversable<Requirement>
53
     */
54
    public function getIterator(): Traversable
55
    {
56
        return new ArrayIterator($this->requirements);
0 ignored issues
show
$this->requirements of type KevinGH\RequirementChecker\list is incompatible with the type array expected by parameter $array of ArrayIterator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        return new ArrayIterator(/** @scrutinizer ignore-type */ $this->requirements);
Loading history...
57
    }
58
59
    public function count(): int
60
    {
61
        return count($this->requirements);
62
    }
63
64
    public function add(Requirement $requirement): void
65
    {
66
        $this->requirements[] = $requirement;
67
    }
68
69
    /**
70
     * Adds a mandatory requirement evaluated lazily.
71
     *
72
     * @param IsFulfilled $checkIsFulfilled whether the requirement is fulfilled; This string is will be evaluated with `eval()` because
73
     *                                      PHP does not support the serialization or the export of closures
74
     * @param string      $testMessage      The message for testing the requirement
75
     * @param string      $helpText         The help text (when null, it will be inferred from $helpHtml, i.e. stripped from HTML tags)
76
     */
77
    public function addRequirement(IsFulfilled $checkIsFulfilled, string $testMessage, string $helpText): void
78
    {
79
        $this->add(new Requirement($checkIsFulfilled, $testMessage, $helpText));
80
    }
81
82
    /**
83
     * Returns all mandatory requirements.
84
     *
85
     * @return list<Requirement>
86
     */
87
    public function getRequirements(): array
88
    {
89
        return $this->requirements;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->requirements returns the type KevinGH\RequirementChecker\list which is incompatible with the type-hinted return array.
Loading history...
90
    }
91
92
    /**
93
     * Returns the PHP configuration file (php.ini) path.
94
     *
95
     * @return false|string php.ini file path
96
     */
97
    public function getPhpIniPath()
98
    {
99
        return $this->phpIniPath;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->phpIniPath also could return the type boolean which is incompatible with the documented return type false|string.
Loading history...
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function evaluateRequirements()
106
    {
107
        return array_reduce(
108
            $this->requirements,
0 ignored issues
show
$this->requirements of type KevinGH\RequirementChecker\list is incompatible with the type array expected by parameter $array of array_reduce(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
            /** @scrutinizer ignore-type */ $this->requirements,
Loading history...
109
            static function (bool $checkPassed, Requirement $requirement): bool {
110
                return $checkPassed && $requirement->isFulfilled();
111
            },
112
            true
113
        );
114
    }
115
}
116