OptimizedImagesType::resolve()   C
last analyzed

Complexity

Conditions 16
Paths 16

Size

Total Lines 82
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 48
c 0
b 0
f 0
dl 0
loc 82
rs 5.5666
cc 16
nc 16
nop 4

How to fix   Long Method    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
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
3
 * @link https://craftcms.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
Coding Style introduced by
Tag value for @link tag indented incorrectly; expected 6 spaces but found 1
Loading history...
4
 * @copyright Copyright (c) Pixel & Tonic, Inc.
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
5
 * @license https://craftcms.github.io/license/
0 ignored issues
show
Coding Style introduced by
@license tag must contain a URL and a license name
Loading history...
Coding Style introduced by
Tag value for @license tag indented incorrectly; expected 3 spaces but found 1
Loading history...
6
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
7
8
namespace nystudio107\imageoptimize\gql\types;
9
10
use craft\gql\base\ObjectType;
11
use GraphQL\Type\Definition\ResolveInfo;
12
use nystudio107\imageoptimize\models\OptimizedImage;
13
14
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
15
 * @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...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
16
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
17
 * @since     1.6.2
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
18
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
19
class OptimizedImagesType extends ObjectType
20
{
21
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $source should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $arguments should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $context should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $resolveInfo should have a doc-comment as per coding-style.
Loading history...
22
     * @inheritdoc
23
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
24
    protected function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed
25
    {
26
        /** @var OptimizedImage $source */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
27
        $fieldName = $resolveInfo->fieldName;
28
29
        switch ($fieldName) {
30
            // Special-case the `src` field with arguments
31
            case 'src':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
32
                $width = $arguments['width'] ?? 0;
33
                return $source->src($width);
34
35
            // Special-case the `srcWebp` field with arguments
36
            case 'srcWebp':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
37
                $width = $arguments['width'] ?? 0;
38
                return $source->srcWebp($width);
39
40
            // Special-case the `srcset` field with arguments
41
            case 'srcset':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
42
                $dpr = $arguments['dpr'] ?? false;
43
                return $source->srcset($dpr);
44
45
            // Special-case the `srcsetMinWidth` field with arguments
46
            case 'srcsetMinWidth':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
47
                $width = $arguments['width'] ?? 0;
48
                $dpr = $arguments['dpr'] ?? false;
49
                return $source->srcsetMinWidth($width, $dpr);
50
51
            // Special-case the `srcsetMaxWidth` field with arguments
52
            case 'srcsetMaxWidth':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
53
                $width = $arguments['width'] ?? 0;
54
                $dpr = $arguments['dpr'] ?? false;
55
                return $source->srcsetMaxWidth($width, $dpr);
56
57
            // Special-case the `srcsetWebp` field with arguments
58
            case 'srcsetWebp':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
59
                $dpr = $arguments['dpr'] ?? false;
60
                return $source->srcsetWebp($dpr);
61
62
            // Special-case the `srcsetMinWidthWebp` field with arguments
63
            case 'srcsetMinWidthWebp':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
64
                $width = $arguments['width'] ?? 0;
65
                $dpr = $arguments['dpr'] ?? false;
66
                return $source->srcsetMinWidthWebp($width, $dpr);
67
68
            // Special-case the `srcsetMaxWidthWebp` field with arguments
69
            case 'srcsetMaxWidthWebp':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
70
                $width = $arguments['width'] ?? 0;
71
                $dpr = $arguments['dpr'] ?? false;
72
                return $source->srcsetMaxWidthWebp($width, $dpr);
73
74
            // Special-case the `maxSrcsetWidth` field
75
            case 'maxSrcsetWidth':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
76
                return $source->maxSrcsetWidth();
77
78
            // Special-case the `placeholderImage` field
79
            case 'placeholderImage':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
80
                return $source->placeholderImage();
81
82
            // Special-case the `placeholderBox` field
83
            case 'placeholderBox':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
84
                $color = $arguments['color'] ?? null;
85
                return $source->placeholderBox($color);
86
87
            // Special-case the `placeholderSilhouette` field
88
            case 'placeholderSilhouette':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
89
                return $source->placeholderSilhouette();
90
91
            // Special-case the `srcUrls` field
92
            case 'srcUrls':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
93
                $result = [];
94
                foreach ($source->optimizedImageUrls as $width => $url) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
95
                    $result[] = ['width' => $width, 'url' => $url];
96
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
97
                return $result;
98
99
            // Special-case the `colorPaletteRgb` field
100
            case 'colorPaletteRgb':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
101
                return $source->colorPaletteRgb();
102
103
            // Default to just returning the field value
104
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
105
                return $source[$fieldName];
106
        }
107
    }
108
}
109