Test Failed
Push — develop ( 2ae3c1...31453c )
by Paul
13:05
created

Controller::filterSchema()   B

Complexity

Conditions 10
Paths 13

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 35
rs 7.6666
cc 10
nc 13
nop 1

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\YoastSEO;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Defaults\RatingSchemaTypeDefaults;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Modules\Schema;
9
10
class Controller extends AbstractController
11
{
12
    /**
13
     * @param array $graph
14
     *
15
     * @filter wpseo_schema_graph
16
     */
17
    public function filterSchema($graph): array
18
    {
19
        $graph = Arr::consolidate($graph);
20
        if (function_exists('is_product') && is_product()) {
21
            return $graph; // skip WooCommerce products
22
        }
23
        $schema = glsr(Schema::class)->generate();
24
        if (empty($schema)) {
25
            return $graph;
26
        }
27
        $allowedTypes = glsr(RatingSchemaTypeDefaults::class)->defaults();
28
        $aggregateRatingSchema = Arr::get($schema, 'aggregateRating');
29
        $reviewSchema = Arr::get($schema, 'review');
30
        foreach ($graph as $key => $item) {
31
            $types = Arr::getAs('array', $item, '@type');
32
            if (empty(array_intersect($types, $allowedTypes))) {
33
                continue;
34
            }
35
            $isReviewType = !empty(array_intersect($types, ['Review', 'ReviewNewsArticle']));
36
            if (!empty($aggregateRatingSchema)) {
37
                if ($isReviewType) {
38
                    $graph[$key]['itemReviewed']['aggregateRating'] = $aggregateRatingSchema;
39
                } else {
40
                    $graph[$key]['aggregateRating'] = $aggregateRatingSchema;
41
                }
42
            }
43
            if (!empty($reviewSchema)) {
44
                if ($isReviewType) {
45
                    $graph[$key]['itemReviewed']['review'] = $reviewSchema;
46
                } else {
47
                    $graph[$key]['review'] = $reviewSchema;
48
                }
49
            }
50
        }
51
        return $graph;
52
    }
53
}
54