Failed Conditions
Branch refactor/kernels (fbf61a)
by Atanas
01:47
created

src/Middleware/HasMiddlewareDefinitionsTrait.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Middleware;
11
12
use WPEmerge\Exceptions\ConfigurationException;
13
14
/**
15
 * Provide middleware definitions.
16
 */
17
trait HasMiddlewareDefinitionsTrait {
18
	/**
19
	 * Middleware available to the application.
20
	 *
21
	 * @var array<string, string>
22
	 */
23
	protected $middleware = [];
24
25
	/**
26
	 * Middleware groups.
27
	 *
28
	 * @var array<string, array<string>>
29
	 */
30
	protected $middleware_groups = [];
31
32
	/**
33
	 * Middleware groups that should have the 'global' group prepended to them.
34
	 *
35
	 * @var array<string, string>
36
	 */
37
	protected $middleware_groups_with_global = [
38
		'web',
39
		'admin',
40
		'ajax',
41
	];
42
43
	/**
44
	 * Register middleware.
45
	 *
46
	 * @codeCoverageIgnore
47
	 * @param  array<string, string> $middleware
48
	 * @return void
49
	 */
50
	public function setMiddleware( $middleware ) {
51
		$this->middleware = $middleware;
52
	}
53
54
	/**
55
	 * Register middleware groups.
56
	 *
57
	 * @codeCoverageIgnore
58
	 * @param  array<string, array<string>> $middleware_groups
59
	 * @return void
60
	 */
61
	public function setMiddlewareGroups( $middleware_groups ) {
62
		$this->middleware_groups = $middleware_groups;
63
	}
64
65
	/**
66
	 * Filter array of middleware into a unique set.
67
	 *
68
	 * @param  array<string> $middleware
69
	 * @return array<string>
70
	 */
71 1
	public function uniqueMiddleware( $middleware ) {
72 1
		return array_values( array_unique( $middleware, SORT_REGULAR ) );
73
	}
74
75
	/**
76
	 * Expand array of middleware into an array of fully qualified class names.
77
	 *
78
	 * @param  array<array> $middleware
79
	 * @return array<array>
80
	 */
81 1
	public function expandMiddleware( $middleware ) {
82 1
		$classes = [];
83
84 1
		foreach ( $middleware as $item ) {
85 1
			$classes = array_merge(
86 1
				$classes,
87 1
				$this->expandMiddlewareMolecule( $item )
0 ignored issues
show
$item of type array is incompatible with the type string expected by parameter $middleware of WPEmerge\Middleware\HasM...andMiddlewareMolecule(). ( Ignorable by Annotation )

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

87
				$this->expandMiddlewareMolecule( /** @scrutinizer ignore-type */ $item )
Loading history...
88 1
			);
89 1
		}
90
91 1
		return $classes;
92
	}
93
94
	/**
95
	 * Expand a middleware group into an array of fully qualified class names.
96
	 *
97
	 * @param  string        $group
98
	 * @return array<array>
99
	 */
100 3
	public function expandMiddlewareGroup( $group ) {
101 3
		if ( ! isset( $this->middleware_groups[ $group ] ) ) {
102 1
			throw new ConfigurationException( 'Unknown middleware group "' . $group . '" used.' );
103
		}
104
105 2
		$middleware = $this->middleware_groups[ $group ];
106
107 2
		if ( in_array( $group, $this->middleware_groups_with_global, true ) ) {
108 1
			$middleware = array_merge( ['global'], $middleware );
109 1
		}
110
111 2
		return $this->expandMiddleware( $middleware );
0 ignored issues
show
It seems like $middleware can also be of type string[]; however, parameter $middleware of WPEmerge\Middleware\HasM...ait::expandMiddleware() does only seem to accept array<mixed,array>, maybe add an additional type check? ( Ignorable by Annotation )

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

111
		return $this->expandMiddleware( /** @scrutinizer ignore-type */ $middleware );
Loading history...
112
	}
113
114
	/**
115
	 * Expand middleware into an array of fully qualified class names and any companion arguments.
116
	 *
117
	 * @param  string       $middleware
118
	 * @return array<array>
119
	 */
120 5
	public function expandMiddlewareMolecule( $middleware ) {
121 5
		$pieces = explode( ':', $middleware, 2 );
122
123 5
		if ( count( $pieces ) > 1 ) {
124 1
			return [array_merge( [$this->expandMiddlewareAtom( $pieces[0] )], explode( ',', $pieces[1] ) )];
125
		}
126
127 4
		if ( isset( $this->middleware_groups[ $middleware ] ) ) {
128 1
			return $this->expandMiddlewareGroup( $middleware );
129
		}
130
131 4
		return [[$this->expandMiddlewareAtom( $middleware )]];
132
	}
133
134
	/**
135
	 * Expand a single middleware a fully qualified class name.
136
	 *
137
	 * @param  string $middleware
138
	 * @return string
139
	 */
140 3
	public function expandMiddlewareAtom( $middleware ) {
141 3
		if ( isset( $this->middleware[ $middleware ] ) ) {
142 1
			return $this->middleware[ $middleware ];
143
		}
144
145 2
		if ( class_exists( $middleware ) ) {
146 1
			return $middleware;
147
		}
148
149 1
		throw new ConfigurationException( 'Unknown middleware "' . $middleware . '" used.' );
150
	}
151
}
152