Issues (46)

src/Middleware/AbstractMiddleware.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Copyright (c) 2017–2019 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017–2019 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Middleware;
12
13
use SimplePie\Configuration as C;
14
use SimplePie\Mixin as Tr;
15
16
/**
17
 * The base middleware class that all other middleware classes extend from. It handles low-level functionality that is
18
 * shared across all middleware classes.
19
 */
20
abstract class AbstractMiddleware implements C\SetLoggerInterface
21
{
22
    use Tr\LoggerTrait;
0 ignored issues
show
The type SimplePie\Mixin\LoggerTrait 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...
23
24
    /**
25
     * A callable which is used to determine whether or not to run this middleware.
26
     *
27
     * @var callable
28
     */
29
    protected $fn;
30
31
    /**
32
     * Constructs a new instance of this class.
33
     *
34
     * Accepts a callable with the following function signature:
35
     *
36
     * ```
37
     * function () {}
38
     * ```
39
     *
40
     * @param callable|null $fn A callable which is used to determine whether or not to run this
41
     *                          middleware. A value of `true` means that the middleware should run.
42
     *                          A value of `false` means that the middleware should NOT run, By
43
     *                          default, the middleware will run.
44
     */
45 562
    public function __construct(?callable $fn = null)
46
    {
47
        $this->fn = $fn ?: static function () {
48
            return true;
49 562
        };
50 562
    }
51
52
    /**
53
     * Checks whether or not a particular property exists on an object, and if not, instantiates it as an array.
54
     *
55
     * @param mixed  $object   An object that you can add ad-hoc properties to. Preferably a `stdClass` object.
56
     * @param string $property The name of the property to check and/or add.
57
     */
58 560
    public function addArrayProperty(&$object, string $property): void
59
    {
60 560
        if (!isset($object->{$property})) {
61 560
            $object->{$property} = [];
62
        }
63 560
    }
64
65
    /**
66
     * Return the value of an associative array key, if it exists. If not, return $default.
67
     *
68
     * @param array  $arr     The associative array to check.
69
     * @param string $key     The key in the associative array to return the value for.
70
     * @param mixed  $default The default value to return if there is no value. The default value is `null`.
71
     */
72 560
    public function get(array $arr, string $key, $default = null)
73
    {
74 560
        return $arr[$key]
75 560
            ?? $default;
76
    }
77
}
78