Passed
Push — develop ( 0538bf...b9d88f )
by Andrew
04:03
created

VitePluginService::init()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
c 5
b 0
f 0
nc 11
nop 0
dl 0
loc 31
rs 8.8333
1
<?php
2
/**
3
 * Vite plugin for Craft CMS 3.x
4
 *
5
 * Allows the use of the Vite.js next generation frontend tooling with Craft CMS
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) 2021 nystudio107
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\pluginvite\services;
12
13
use Craft;
14
use craft\web\AssetBundle;
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   Vite
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 VitePluginService extends ViteService
22
{
23
    // Constants
24
    // =========================================================================
25
26
    const MANIFEST_FILE_NAME = 'manifest.json';
27
28
    // Public Properties
29
    // =========================================================================
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
32
     * @var string AssetBundle class name to get the published URLs from
33
     */
34
    public $assetClass;
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @var string The environment variable to look for in order to enable the devServer; the value doesn't matter,
38
     *              it just needs to exist
39
     */
40
    public $pluginDevServerEnvVar = 'VITE_PLUGIN_DEVSERVER';
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @inheritDoc
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public function init()
46
    {
47
        parent::init();
48
        // Do nothing for console requests
49
        $request = Craft::$app->getRequest();
50
        if ($request->getIsConsoleRequest()) {
51
            return;
52
        }
53
        $this->invalidateCaches();
54
        // See if the $pluginDevServerEnvVar env var exists, and if not, don't run off of the dev server
55
        $useDevServer = getenv($this->pluginDevServerEnvVar);
56
        if ($useDevServer === false) {
57
            $this->useDevServer = false;
58
        }
59
        // If we have no asset bundle class, or the dev server is running, don't swap in our `/cpresources/` paths
60
        if (!$this->assetClass || $this->devServerRunning()) {
61
            return;
62
        }
63
        // If we're in a plugin, make sure the caches are unique
64
        if ($this->assetClass) {
65
            $this->cacheKeySuffix = $this->assetClass;
66
        }
67
        // Map the $manifestPath and $serverPublic to the hashed `/cpresources/` path & URL for our AssetBundle
68
        $bundle = new $this->assetClass;
69
        $baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl(
70
            $bundle->sourcePath,
71
            true
0 ignored issues
show
Unused Code introduced by
The call to yii\web\AssetManager::getPublishedUrl() has too many arguments starting with true. ( Ignorable by Annotation )

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

71
        /** @scrutinizer ignore-call */ 
72
        $baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
        );
73
        $this->manifestPath = $this->createUrl($bundle->sourcePath, self::MANIFEST_FILE_NAME);
74
        if ($baseAssetsUrl !== false) {
75
            $this->serverPublic = $baseAssetsUrl;
76
        }
77
    }
78
}
79