Skip to content

Commit 21caaba

Browse files
committed
fix: Code style fixes
1 parent d618aab commit 21caaba

File tree

2 files changed

+97
-80
lines changed

2 files changed

+97
-80
lines changed

src/Utils/oblak-wp-hook-utils.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php //phpcs:disable Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, SlevomatCodingStandard.ControlStructures.AssignmentInCondition.AssignmentInCondition
22
/**
33
* Utility functions for the WP hooks.
44
*
@@ -20,10 +20,7 @@ function xwp_invoke_hooked_methods( object|string $obj, bool $all = false ) {
2020

2121
foreach ( $methods as $method => $hook_data ) {
2222
foreach ( $hook_data['hooks'] as $hook ) {
23-
$hook->run_hook(
24-
array( $obj, $method ),
25-
$hook_data['args']
26-
);
23+
$hook->run_hook( array( $obj, $method ), $hook_data['args'] );
2724
}
2825
}
2926
}
@@ -39,20 +36,20 @@ function xwp_get_hooked_methods( object|string $obj, bool $all = false ): array
3936
$reflector = new ReflectionClass( $obj );
4037
$methods = array_filter(
4138
$reflector->getMethods( xwp_get_hookable_method_types( $obj ) ),
42-
fn( $m ) => $all || $m->class === $reflector->getName()
39+
static fn( $m ) => $all || $m->class === $reflector->getName()
4340
);
4441

4542
return array_filter(
4643
wp_array_flatmap(
4744
$methods,
48-
fn( $m )=> array(
45+
static fn( $m ) => array(
4946
$m->getName() => array(
50-
'hooks' => xwp_get_hook_decorators( $m ),
51-
'args' => $m->getNumberOfParameters(),
47+
'args' => $m->getNumberOfParameters(),
48+
'hooks' => xwp_get_hook_decorators( $m ),
5249
),
5350
),
5451
),
55-
fn( $m ) => $m['hooks']
52+
static fn( $m ) => $m['hooks']
5653
);
5754
}
5855

@@ -65,13 +62,13 @@ function xwp_get_hooked_methods( object|string $obj, bool $all = false ): array
6562
*/
6663
function xwp_get_hook_decorators(
6764
ReflectionFunctionAbstract|ReflectionClass $thing,
68-
string $att = Base_Hook::class
65+
string $att = Base_Hook::class,
6966
): array|false {
7067
$decorators = array_filter(
7168
array_map(
72-
fn( $d ) => $d?->newInstance() ?? false,
73-
$thing->getAttributes( $att, ReflectionAttribute::IS_INSTANCEOF )
74-
)
69+
static fn( $d ) => $d?->newInstance() ?? false,
70+
$thing->getAttributes( $att, ReflectionAttribute::IS_INSTANCEOF ),
71+
),
7572
);
7673

7774
return $decorators ? $decorators : false;
@@ -90,9 +87,30 @@ function xwp_get_hookable_method_types( object|string $obj ): int {
9087
$method_types |= ReflectionMethod::IS_STATIC;
9188
}
9289

93-
if ( in_array( '\Oblak\WP\Traits\Accessible_Hook_Methods', class_uses_deep( $obj ), true ) ) {
90+
if ( in_array( '\Oblak\WP\Traits\Accessible_Hook_Methods', xwp_class_uses_deep( $obj ), true ) ) {
9491
$method_types |= ReflectionMethod::IS_PRIVATE | ReflectionMethod::IS_PROTECTED;
9592
}
9693

9794
return $method_types;
9895
}
96+
97+
/**
98+
* Get all the traits used by a class.
99+
*
100+
* @param string|object $object_or_class Class or object to get the traits for.
101+
* @param bool $autoload Whether to allow this function to load the class automatically through the __autoload() magic method.
102+
* @return array Array of traits.
103+
*/
104+
function xwp_class_uses_deep( string|object $object_or_class, bool $autoload = true ) {
105+
$traits = array();
106+
107+
do {
108+
$traits = \array_merge( \class_uses( $object_or_class, $autoload ), $traits );
109+
} while ( $object_or_class = \get_parent_class( $object_or_class ) );
110+
111+
foreach ( $traits as $trait ) {
112+
$traits = \array_merge( \class_uses( $trait, $autoload ), $traits );
113+
}
114+
115+
return \array_values( \array_unique( $traits ) );
116+
}

src/Utils/oblak-wp-metadata-utils.php

Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php //phpcs:disable Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
1+
<?php //phpcs:disable SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions
22
/**
33
* WordPress metadata utils
44
*
@@ -26,14 +26,17 @@ function get_decorators( $class_or_obj, $decorator, bool $all = false ) {
2626
$decorators = array();
2727

2828
while ( $class_or_obj ) {
29-
$decorators = array_merge(
29+
$decorators = \array_merge(
3030
$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+
),
3538
);
36-
$class_or_obj = $all ? get_parent_class( $class_or_obj ) : null;
39+
$class_or_obj = $all ? \get_parent_class( $class_or_obj ) : null;
3740
}
3841

3942
return $decorators;
@@ -52,28 +55,28 @@ function parse_annotations( ReflectionMethod &$method, ?array $needed_keys = nul
5255
return null;
5356
}
5457

55-
preg_match_all( '/@([a-z]+?)\s+(.*?)\n/i', $doc, $annotations );
58+
\preg_match_all( '/@([a-z]+?)\s+(.*?)\n/i', $doc, $annotations );
5659

57-
if ( ! isset( $annotations[1] ) || 0 === count( $annotations[1] ) ) {
60+
if ( ! isset( $annotations[1] ) || 0 === \count( $annotations[1] ) ) {
5861
return array();
5962
}
6063

6164
$needed_keys ??= array( 'hook', 'type' );
6265

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.
6871
),
69-
array( 'args' => $method->getNumberOfParameters() ) // Add the number of params.
72+
array( 'args' => $method->getNumberOfParameters() ), // Add the number of params.
7073
),
71-
fn( $v ) => '' !== $v
74+
static fn( $v ) => '' !== $v,
7275
);
7376

7477
// 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 )
7780
? $annotations
7881
: array();
7982
}
@@ -86,17 +89,24 @@ function parse_annotations( ReflectionMethod &$method, ?array $needed_keys = nul
8689
*/
8790
function get_hook_priority( int|string|null $priority_prop = null ): int {
8891
$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 ) ) {
9297
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 );
95102

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
99107
}
108+
109+
return 10;
100110
}
101111

102112
/**
@@ -110,26 +120,35 @@ function get_hook_priority( int|string|null $priority_prop = null ): int {
110120
function get_class_hooks( $class_or_obj, ?array $needed_keys = null, bool $all = false ): array {
111121
$reflector = new ReflectionClass( $class_or_obj );
112122

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(
116126
$hook_args,
117127
array(
118-
'hook' => null,
119-
'args' => 0,
120-
)
128+
'args' => 0,
129+
'hook' => null,
130+
),
121131
),
122-
array_filter(
123-
wp_array_flatmap(
124-
array_filter(
132+
\array_filter(
133+
\wp_array_flatmap(
134+
\array_filter(
125135
$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+
),
127143
),
128-
fn( $method ) => array( $method->getName() => parse_annotations( $method, $needed_keys ) ),
129-
)
130-
)
144+
),
145+
),
131146
),
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+
) )
133152
);
134153
}
135154

@@ -143,8 +162,11 @@ function invoke_class_hooks( $class_or_obj, ?array $hooks = null ) {
143162
$hooks ??= get_class_hooks( $class_or_obj );
144163

145164
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+
);
148170

149171
foreach ( $hook_names as $index => $hook_name ) {
150172
"add_{$hook_data['type']}"(
@@ -156,26 +178,3 @@ function invoke_class_hooks( $class_or_obj, ?array $hooks = null ) {
156178
}
157179
}
158180
}
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

Comments
 (0)