Json   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 75
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeJsonLdArray() 0 6 1
A processData() 0 8 1
A encode() 0 14 2
A changeKey() 0 10 2
1
<?php
2
/**
3
 * Recipe plugin for Craft CMS 3.x
4
 *
5
 * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial
6
 * conversion, portion calculation, and JSON-LD microdata support
7
 *
8
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
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...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\recipe\helpers;
13
14
use Craft;
15
16
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
17
 * @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...
18
 * @package   Recipe
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
19
 * @since     1.0.0
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...
20
 */
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...
21
class Json extends \craft\helpers\Json
22
{
23
    // Static Properties
24
    // =========================================================================
25
26
    protected static int $recursionLevel;
27
28
    // Static Methods
29
    // =========================================================================
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $options should have a doc-comment as per coding-style.
Loading history...
32
     * @inheritdoc
33
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
34
    public static function encode(
35
        $value,
36
        $options =
0 ignored issues
show
Coding Style introduced by
Multi-line assignments must have the equal sign on the second line
Loading history...
37
        JSON_UNESCAPED_UNICODE
38
        | JSON_UNESCAPED_SLASHES,
39
    ): string {
40
        // If `devMode` is enabled, make the JSON-LD human-readable
41
        if (Craft::$app->getConfig()->getGeneral()->devMode) {
42
            $options |= JSON_PRETTY_PRINT;
43
        }
44
45
        self::$recursionLevel = 0;
46
47
        return parent::encode($value, $options);
48
    }
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expressions should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expPrefix should have a doc-comment as per coding-style.
Loading history...
51
     * @inheritdoc
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
53
    protected static function processData($data, &$expressions, $expPrefix)
54
    {
55
        ++self::$recursionLevel;
56
        $result = parent::processData($data, $expressions, $expPrefix);
57
        --self::$recursionLevel;
58
        static::normalizeJsonLdArray($result, self::$recursionLevel);
59
60
        return $result;
61
    }
62
63
64
    // Private Methods
65
    // =========================================================================
66
67
    /**
68
     * Normalize the JSON-LD array recursively to remove empty values, change
69
     * 'type' to '@type' and have it be the first item in the array
70
     *
71
     * @param $array
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
72
     * @param $depth
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
73
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
74
    protected static function normalizeJsonLdArray(&$array, $depth): void
0 ignored issues
show
Unused Code introduced by
The parameter $depth 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

74
    protected static function normalizeJsonLdArray(&$array, /** @scrutinizer ignore-unused */ $depth): void

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...
75
    {
76
        $array = array_filter($array);
77
        $array = self::changeKey($array, 'context', '@context');
78
        $array = self::changeKey($array, 'type', '@type');
79
        ksort($array);
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $array should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $oldKey should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $newKey should have a doc-comment as per coding-style.
Loading history...
83
     * Replace key values without reordering the array or converting numeric
84
     * keys to associative keys (which unset() does)
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    protected static function changeKey(array $array, string $oldKey, string $newKey): array
87
    {
88
        if (!array_key_exists($oldKey, $array)) {
89
            return $array;
90
        }
91
92
        $keys = array_keys($array);
93
        $keys[array_search($oldKey, $keys)] = $newKey;
94
95
        return array_combine($keys, $array);
96
    }
97
}
98