Similar   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 1
eloc 26
c 3
b 0
f 0
dl 0
loc 68
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 30 1
1
<?php
2
/**
3
 * Similar plugin for Craft CMS 3.x
4
 *
5
 * Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.
6
 *
7
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107.com
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
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...
10
11
namespace nystudio107\similar;
12
13
use Craft;
14
use craft\base\Element;
15
use craft\base\Plugin;
16
use craft\elements\db\ElementQuery;
17
use craft\events\PopulateElementEvent;
18
use craft\web\twig\variables\CraftVariable;
19
use nystudio107\similar\behaviors\CountBehavior;
20
use nystudio107\similar\services\ServicesTrait;
21
use nystudio107\similar\variables\SimilarVariable;
22
use yii\base\Event;
23
24
/**
25
 * Class Similar
26
 *
27
 * @author    nystudio107.com
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...
28
 * @package   Similar
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
29
 * @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...
30
 */
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...
31
class Similar extends Plugin
32
{
33
    // Traits
34
    // =========================================================================
35
36
    use ServicesTrait;
37
38
    // Static Properties
39
    // =========================================================================
40
41
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
42
     * @var ?Similar
43
     */
44
    public static ?Similar $plugin = null;
45
46
    // Public Properties
47
    // =========================================================================
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @var string
51
     */
52
    public string $schemaVersion = '1.0.0';
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @var bool
56
     */
57
    public bool $hasCpSection = false;
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @var bool
60
     */
61
    public bool $hasCpSettings = false;
62
63
    // Public Methods
64
    // =========================================================================
65
66
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
67
     * @inheritdoc
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public function init(): void
70
    {
71
        parent::init();
72
        self::$plugin = $this;
73
74
        Event::on(
75
            CraftVariable::class,
76
            CraftVariable::EVENT_INIT,
77
            static function(Event $event): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
78
                /** @var CraftVariable $variable */
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...
79
                $variable = $event->sender;
80
                $variable->set('similar', SimilarVariable::class);
81
            }
82
        );
83
        Event::on(
84
            ElementQuery::class,
85
            ElementQuery::EVENT_AFTER_POPULATE_ELEMENT,
86
            static function(PopulateElementEvent $event): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
87
                /** @var Element $element */
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...
88
                $element = $event->element;
89
                $element->attachBehavior('myCountBehavior', CountBehavior::class);
90
            });
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
91
92
        Craft::info(
93
            Craft::t(
94
                'similar',
95
                '{name} plugin loaded',
96
                ['name' => $this->name]
97
            ),
98
            __METHOD__
99
        );
100
    }
101
102
    // Protected Methods
103
    // =========================================================================
104
}
105