Passed
Push — develop ( 763cf7...df804f )
by Andrew
06:06
created

CraftQLSchemaHelper::getContainerArrays()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 71
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 40
nc 8
nop 3
dl 0
loc 71
rs 8.6577
c 0
b 0
f 0

How to fix   Long Method   

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
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * @link      https://nystudio107.com/
6
 * @copyright Copyright (c) 2019 nystudio107
7
 * @license   https://nystudio107.com/license
0 ignored issues
show
Coding Style introduced by
@license tag must contain a URL and a license name
Loading history...
8
 */
9
10
namespace nystudio107\seomatic\helpers;
11
12
use Craft;
13
use craft\base\Element;
14
use craft\elements\Entry;
15
16
use nystudio107\seomatic\helpers\Container as ContainerHelper;
17
use nystudio107\seomatic\models\MetaJsonLdContainer;
18
use nystudio107\seomatic\models\MetaLinkContainer;
19
use nystudio107\seomatic\models\MetaScriptContainer;
20
use nystudio107\seomatic\models\MetaTitleContainer;
21
use nystudio107\seomatic\models\MetaTagContainer;
22
23
use markhuot\CraftQL\Events\AlterSchemaFields;
0 ignored issues
show
Bug introduced by
The type markhuot\CraftQL\Events\AlterSchemaFields was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
/**
26
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
27
 * @package   Seomatic
28
 * @since     3.2.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
29
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
30
class CraftQLSchemaHelper
31
{
32
    // Constants
33
    // =========================================================================
34
35
    const CACHE_KEY = 'seomatic_metacontroller_';
36
37
    // Public Methods
38
    // =========================================================================
39
40
    public static function handle(AlterSchemaFields $event)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
41
    {
42
        // Create the root object
43
        $seomaticField = $event->schema->createObjectType('seomaticData');
44
        // Add metaTitleContainer
45
        $seomaticField->addStringField('metaTitleContainer')->resolve(function (Element $element) {
46
            // $root contains the data returned by the field below
47
            $result = ContainerHelper::getContainerArrays(
48
                [MetaTitleContainer::CONTAINER_TYPE],
49
                $element->uri,
0 ignored issues
show
Bug introduced by
It seems like $element->uri can also be of type null; however, parameter $uri of nystudio107\seomatic\hel...r::getContainerArrays() does only seem to accept string, 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

49
                /** @scrutinizer ignore-type */ $element->uri,
Loading history...
50
                $element->siteId,
51
                false
52
            );
53
54
            return $result[MetaTitleContainer::CONTAINER_TYPE];
55
        });
56
        // Add metaTagContainer
57
        $seomaticField->addStringField('metaTagContainer')->resolve(function (Element $element) {
58
            // $root contains the data returned by the field below
59
            $result = ContainerHelper::getContainerArrays(
60
                [MetaTagContainer::CONTAINER_TYPE],
61
                $element->uri,
0 ignored issues
show
Bug introduced by
It seems like $element->uri can also be of type null; however, parameter $uri of nystudio107\seomatic\hel...r::getContainerArrays() does only seem to accept string, 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

61
                /** @scrutinizer ignore-type */ $element->uri,
Loading history...
62
                $element->siteId,
63
                false
64
            );
65
66
            return $result[MetaTagContainer::CONTAINER_TYPE];
67
        });
68
        // Add metaLinkContainer
69
        $seomaticField->addStringField('metaLinkContainer')->resolve(function (Element $element) {
70
            // $root contains the data returned by the field below
71
            $result = ContainerHelper::getContainerArrays(
72
                [MetaLinkContainer::CONTAINER_TYPE],
73
                $element->uri,
0 ignored issues
show
Bug introduced by
It seems like $element->uri can also be of type null; however, parameter $uri of nystudio107\seomatic\hel...r::getContainerArrays() does only seem to accept string, 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

73
                /** @scrutinizer ignore-type */ $element->uri,
Loading history...
74
                $element->siteId,
75
                false
76
            );
77
78
            return $result[MetaLinkContainer::CONTAINER_TYPE];
79
        });
80
        // Add metaScriptContainer
81
        $seomaticField->addStringField('metaScriptContainer')->resolve(function (Element $element) {
82
            // $root contains the data returned by the field below
83
            $result = ContainerHelper::getContainerArrays(
84
                [MetaScriptContainer::CONTAINER_TYPE],
85
                $element->uri,
0 ignored issues
show
Bug introduced by
It seems like $element->uri can also be of type null; however, parameter $uri of nystudio107\seomatic\hel...r::getContainerArrays() does only seem to accept string, 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

85
                /** @scrutinizer ignore-type */ $element->uri,
Loading history...
86
                $element->siteId,
87
                false
88
            );
89
            Craft::dd($result);
90
91
            return $result[MetaScriptContainer::CONTAINER_TYPE];
92
        });
93
        // Add metaJsonLdContainer
94
        $seomaticField->addStringField('metaJsonLdContainer')->resolve(function (Element $element) {
95
            // $root contains the data returned by the field below
96
            $result = ContainerHelper::getContainerArrays(
97
                [MetaJsonLdContainer::CONTAINER_TYPE],
98
                $element->uri,
0 ignored issues
show
Bug introduced by
It seems like $element->uri can also be of type null; however, parameter $uri of nystudio107\seomatic\hel...r::getContainerArrays() does only seem to accept string, 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

98
                /** @scrutinizer ignore-type */ $element->uri,
Loading history...
99
                $element->siteId,
100
                false
101
            );
102
103
            return $result[MetaJsonLdContainer::CONTAINER_TYPE];
104
        });
105
106
        $event->schema->addField('seomatic')
107
            ->arguments(function (\markhuot\CraftQL\Builders\Field $field) {
0 ignored issues
show
Bug introduced by
The type markhuot\CraftQL\Builders\Field was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
108
                $field->addIntArgument('siteId');
109
                $field->addStringArgument('uri')->nonNull();
110
            })
111
            ->type($seomaticField)
112
            ->resolve(function ($root, $args, $context, $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

112
            ->resolve(function ($root, $args, /** @scrutinizer ignore-unused */ $context, $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed. ( Ignorable by Annotation )

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

112
            ->resolve(function ($root, $args, $context, /** @scrutinizer ignore-unused */ $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
                // Return an entry that meets the passed in criteria.
114
                $criteria = Entry::find()
115
                    ->uri($args['uri']);
116
                if (!empty($args['siteId'])) {
117
                    $criteria->siteId($args['siteId']);
118
                }
119
120
                return $criteria->one();
121
            });
122
    }
123
}
124