1
- <?php //phpcs:disable Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
1
+ <?php //phpcs:disable SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions
2
2
/**
3
3
* WordPress metadata utils
4
4
*
@@ -26,14 +26,17 @@ function get_decorators( $class_or_obj, $decorator, bool $all = false ) {
26
26
$ decorators = array ();
27
27
28
28
while ( $ class_or_obj ) {
29
- $ decorators = array_merge (
29
+ $ decorators = \ array_merge (
30
30
$ decorators ,
31
- array_map (
32
- fn ( $ d ) => $ d ?->newInstance(),
33
- ( new ReflectionClass ( $ class_or_obj ) )?->getAttributes( $ decorator , ReflectionAttribute::IS_INSTANCEOF )
34
- )
31
+ \array_map (
32
+ static fn ( $ d ) => $ d ?->newInstance(),
33
+ ( new ReflectionClass ( $ class_or_obj ) )?->getAttributes(
34
+ $ decorator ,
35
+ ReflectionAttribute::IS_INSTANCEOF ,
36
+ ),
37
+ ),
35
38
);
36
- $ class_or_obj = $ all ? get_parent_class ( $ class_or_obj ) : null ;
39
+ $ class_or_obj = $ all ? \ get_parent_class ( $ class_or_obj ) : null ;
37
40
}
38
41
39
42
return $ decorators ;
@@ -52,28 +55,28 @@ function parse_annotations( ReflectionMethod &$method, ?array $needed_keys = nul
52
55
return null ;
53
56
}
54
57
55
- preg_match_all ( '/@([a-z]+?)\s+(.*?)\n/i ' , $ doc , $ annotations );
58
+ \ preg_match_all ( '/@([a-z]+?)\s+(.*?)\n/i ' , $ doc , $ annotations );
56
59
57
- if ( ! isset ( $ annotations [1 ] ) || 0 === count ( $ annotations [1 ] ) ) {
60
+ if ( ! isset ( $ annotations [1 ] ) || 0 === \ count ( $ annotations [1 ] ) ) {
58
61
return array ();
59
62
}
60
63
61
64
$ needed_keys ??= array ( 'hook ' , 'type ' );
62
65
63
- $ annotations = array_filter (
64
- array_merge ( // Merge the parsed annotations with number of params from the method.
65
- array_combine ( // Combine the annotations with their values.
66
- array_map ( 'trim ' , $ annotations [1 ] ), // Trim the keys.
67
- array_map ( 'trim ' , $ annotations [2 ] ) // Trim the values.
66
+ $ annotations = \ array_filter (
67
+ \ array_merge ( // Merge the parsed annotations with number of params from the method.
68
+ \ array_combine ( // Combine the annotations with their values.
69
+ \ array_map ( 'trim ' , $ annotations [1 ] ), // Trim the keys.
70
+ \ array_map ( 'trim ' , $ annotations [2 ] ), // Trim the values.
68
71
),
69
- array ( 'args ' => $ method ->getNumberOfParameters () ) // Add the number of params.
72
+ array ( 'args ' => $ method ->getNumberOfParameters () ), // Add the number of params.
70
73
),
71
- fn ( $ v ) => '' !== $ v
74
+ static fn ( $ v ) => '' !== $ v,
72
75
);
73
76
74
77
// If the number of found annotations doesn't match the number of needed keys -> gtfo.
75
- return count ( $ annotations ) >= count ( $ needed_keys ) &&
76
- count ( array_intersect ( $ needed_keys , array_keys ( $ annotations ) ) ) >= count ( $ needed_keys )
78
+ return \ count ( $ annotations ) >= \ count ( $ needed_keys ) &&
79
+ \ count ( \ array_intersect ( $ needed_keys , \ array_keys ( $ annotations ) ) ) >= \ count ( $ needed_keys )
77
80
? $ annotations
78
81
: array ();
79
82
}
@@ -86,17 +89,24 @@ function parse_annotations( ReflectionMethod &$method, ?array $needed_keys = nul
86
89
*/
87
90
function get_hook_priority ( int |string |null $ priority_prop = null ): int {
88
91
$ priority_prop ??= 10 ;
89
- if ( is_numeric ( $ priority_prop ) ) {
90
- return intval ( $ priority_prop );
91
- } elseif ( Constants::get_constant ( $ priority_prop ) ) {
92
+ if ( \is_numeric ( $ priority_prop ) ) {
93
+ return \intval ( $ priority_prop );
94
+ }
95
+
96
+ if ( Constants::get_constant ( $ priority_prop ) ) {
92
97
return Constants::get_constant ( $ priority_prop );
93
- } elseif ( str_starts_with ( $ priority_prop , 'filter: ' ) ) {
94
- $ filter_data = explode ( ': ' , $ priority_prop );
98
+ }
99
+
100
+ if ( \str_starts_with ( $ priority_prop , 'filter: ' ) ) {
101
+ $ filter_data = \explode ( ': ' , $ priority_prop );
95
102
96
- return apply_filters ( $ filter_data [1 ], $ filter_data [2 ] ?? 10 ); //phpcs:ignore WooCommerce.Commenting.HookComment
97
- } else {
98
- return 10 ;
103
+ return \apply_filters (
104
+ $ filter_data [1 ],
105
+ $ filter_data [2 ] ?? 10 ,
106
+ ); //phpcs:ignore WooCommerce.Commenting.HookComment
99
107
}
108
+
109
+ return 10 ;
100
110
}
101
111
102
112
/**
@@ -110,26 +120,35 @@ function get_hook_priority( int|string|null $priority_prop = null ): int {
110
120
function get_class_hooks ( $ class_or_obj , ?array $ needed_keys = null , bool $ all = false ): array {
111
121
$ reflector = new ReflectionClass ( $ class_or_obj );
112
122
113
- return array_filter (
114
- array_map (
115
- fn ( $ hook_args ) => wp_parse_args (
123
+ return \ array_filter (
124
+ \ array_map (
125
+ static fn ( $ hook_args ) => \ wp_parse_args (
116
126
$ hook_args ,
117
127
array (
118
- ' hook ' => null ,
119
- ' args ' => 0 ,
120
- )
128
+ ' args ' => 0 ,
129
+ ' hook ' => null ,
130
+ ),
121
131
),
122
- array_filter (
123
- wp_array_flatmap (
124
- array_filter (
132
+ \ array_filter (
133
+ \ wp_array_flatmap (
134
+ \ array_filter (
125
135
$ reflector ->getMethods ( ReflectionMethod::IS_PUBLIC ) ?? array (),
126
- fn ( $ method ) => $ all || $ method ->class === $ reflector ->getName ()
136
+ static fn ( $ method ) => $ all || $ method ->class === $ reflector ->getName ()
137
+ ),
138
+ static fn ( $ method ) => array (
139
+ $ method ->getName () => parse_annotations (
140
+ $ method ,
141
+ $ needed_keys ,
142
+ ),
127
143
),
128
- fn ( $ method ) => array ( $ method ->getName () => parse_annotations ( $ method , $ needed_keys ) ),
129
- )
130
- )
144
+ ),
145
+ ),
131
146
),
132
- fn ( $ h ) => is_null ( $ needed_keys ) || ( count ( array_intersect ( $ needed_keys , array_keys ( $ h ) ) ) >= count ( $ needed_keys ) )
147
+ static fn ( $ h ) => \is_null ( $ needed_keys ) || ( \count (
148
+ \array_intersect ( $ needed_keys , \array_keys ( $ h ) ),
149
+ ) >= \count (
150
+ $ needed_keys ,
151
+ ) )
133
152
);
134
153
}
135
154
@@ -143,8 +162,11 @@ function invoke_class_hooks( $class_or_obj, ?array $hooks = null ) {
143
162
$ hooks ??= get_class_hooks ( $ class_or_obj );
144
163
145
164
foreach ( $ hooks as $ function => $ hook_data ) {
146
- $ hook_names = array_map ( 'trim ' , explode ( ', ' , $ hook_data ['hook ' ] ) );
147
- $ hook_priorities = array_map ( fn ( $ p ) => get_hook_priority ( $ p ), explode ( ', ' , $ hook_data ['priority ' ] ?? '' ) );
165
+ $ hook_names = \array_map ( 'trim ' , \explode ( ', ' , $ hook_data ['hook ' ] ) );
166
+ $ hook_priorities = \array_map (
167
+ static fn ( $ p ) => get_hook_priority ( $ p ),
168
+ \explode ( ', ' , $ hook_data ['priority ' ] ?? '' ),
169
+ );
148
170
149
171
foreach ( $ hook_names as $ index => $ hook_name ) {
150
172
"add_ {$ hook_data ['type ' ]}" (
@@ -156,26 +178,3 @@ function invoke_class_hooks( $class_or_obj, ?array $hooks = null ) {
156
178
}
157
179
}
158
180
}
159
-
160
- /**
161
- * Get all the traits used by a class.
162
- *
163
- * @param string|object $object_or_class Class or object to get the traits for.
164
- * @param bool $autoload Whether to allow this function to load the class automatically through the __autoload() magic method.
165
- * @return array Array of traits.
166
- */
167
- function class_uses_deep ( string |object $ object_or_class , bool $ autoload = true ) {
168
- $ traits = array ();
169
-
170
- do {
171
- $ traits = array_merge ( class_uses ( $ object_or_class , $ autoload ), $ traits );
172
- } while ( $ object_or_class = get_parent_class ( $ object_or_class ) );
173
-
174
- foreach ( $ traits as $ trait => $ same ) {
175
-
176
- $ traits = array_merge ( class_uses ( $ trait , $ autoload ), $ traits );
177
-
178
- }
179
-
180
- return array_unique ( $ traits );
181
- }
0 commit comments