Conditions | 10 |
Paths | 8 |
Total Lines | 119 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
15 | function get(int $postId, array $args = []): array |
||
16 | { |
||
17 | $defaultArgs = [ |
||
18 | 'ep_integrate' => defined('EP_VERSION'), |
||
19 | 'post_types' => ['post'], |
||
20 | 'taxonomies' => ['category'], |
||
21 | 'terms' => [], |
||
22 | 'terms_not_in' => [], |
||
23 | 'limit' => 10, |
||
24 | ]; |
||
25 | |||
26 | $args = wp_parse_args($args, $defaultArgs); |
||
27 | |||
28 | /** |
||
29 | * Control the args. |
||
30 | * |
||
31 | * @param array $args |
||
32 | * @param int $postId |
||
33 | */ |
||
34 | $args = apply_filters('helick_related_posts_args', $args, $postId); |
||
35 | |||
36 | $transient = sprintf('helick_related_posts_%d_%s', $postId, hash('md5', json_encode((array)$args))); |
||
37 | |||
38 | $relatedPostIds = get_transient($transient); |
||
39 | |||
40 | if (is_array($relatedPostIds)) { |
||
41 | return $relatedPostIds; |
||
42 | } |
||
43 | |||
44 | $manualRelatedPostIds = array_column((array)carbon_get_post_meta($postId, 'helick_related_posts'), 'id'); |
||
45 | $manualRelatedPostIds = array_map('intval', $manualRelatedPostIds); |
||
46 | |||
47 | $limit = $args['limit'] - count($manualRelatedPostIds); |
||
48 | |||
49 | if ($limit > 0) { |
||
50 | $queryArgs = [ |
||
51 | 'ep_integrate' => $args['ep_integrate'], |
||
52 | 'fields' => 'ids', |
||
53 | 'post_type' => $args['post_types'], |
||
54 | 'post_status' => 'publish', |
||
55 | 'post__not_in' => array_merge([$postId], $manualRelatedPostIds), |
||
56 | 'posts_per_page' => $limit, |
||
57 | 'tax_query' => [], |
||
58 | 'order' => 'DESC', |
||
59 | ]; |
||
60 | |||
61 | $terms = $args['terms']; |
||
62 | |||
63 | if (empty($terms)) { |
||
64 | $terms = wp_get_object_terms($postId, $args['taxonomies']); |
||
65 | if (is_wp_error($terms)) { |
||
66 | $terms = []; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | foreach ($terms as $term) { |
||
71 | if (!isset($queryArgs['tax_query'][$term->taxonomy])) { |
||
72 | $queryArgs['tax_query'][$term->taxonomy] = [ |
||
73 | 'taxonomy' => $term->taxonomy, |
||
74 | 'field' => 'term_id', |
||
75 | 'terms' => [], |
||
76 | ]; |
||
77 | } |
||
78 | |||
79 | array_push($queryArgs['tax_query'][$term->taxonomy]['terms'], $term->term_id); |
||
80 | } |
||
81 | |||
82 | foreach ($args['terms_not_in'] as $term) { |
||
83 | if (!isset($queryArgs['tax_query']['not_' . $term->taxonomy])) { |
||
84 | $queryArgs['tax_query']['not_' . $term->taxonomy] = [ |
||
85 | 'taxonomy' => $term->taxonomy, |
||
86 | 'field' => 'term_id', |
||
87 | 'terms' => [], |
||
88 | 'operator' => 'NOT IN', |
||
89 | ]; |
||
90 | } |
||
91 | |||
92 | array_push($queryArgs['tax_query']['not_' . $term->taxonomy]['terms'], $term->term_id); |
||
93 | } |
||
94 | |||
95 | $queryArgs['tax_query'] = array_values($queryArgs['tax_query']); |
||
96 | $queryArgs['tax_query']['relation'] = 'OR'; |
||
97 | |||
98 | if ($args['ep_integrate']) { |
||
99 | $relatedTaxonomies = array_map(function ($taxonomy) { |
||
100 | return "terms.{$taxonomy}.name"; |
||
101 | }, $args['taxonomies']); |
||
102 | |||
103 | $relatedFields = apply_filters('helick_related_posts_fields', array_merge([ |
||
104 | 'post_title', |
||
105 | 'post_content', |
||
106 | ], $relatedTaxonomies)); |
||
107 | |||
108 | $queryArgs['more_like'] = $postId; |
||
109 | $queryArgs['more_like_fields'] = $relatedFields; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Control the query args. |
||
114 | * |
||
115 | * @param array $queryArgs |
||
116 | * @param int $postId |
||
117 | * @param array $args |
||
118 | */ |
||
119 | $queryArgs = apply_filters('helick_related_posts_query_args', $queryArgs, $postId, $args); |
||
120 | |||
121 | $query = new WP_Query($queryArgs); |
||
122 | wp_reset_postdata(); |
||
123 | |||
124 | $relatedPostIds = array_merge($manualRelatedPostIds, $query->posts); |
||
125 | $relatedPostIds = array_map('intval', $relatedPostIds); |
||
126 | $relatedPostIds = array_unique($relatedPostIds); |
||
127 | } |
||
128 | |||
129 | $relatedPostIds = array_slice($relatedPostIds, 0, $args['limit']); |
||
130 | |||
131 | set_transient($transient, $relatedPostIds, HOUR_IN_SECONDS); |
||
132 | |||
133 | return $relatedPostIds; |
||
134 | } |
||
135 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths