WebpackPageControllerExtension   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 126
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A IsNotWebpackDevServer() 0 4 2
A IsWebpackDevServer() 0 17 4
A WebpackBaseURL() 0 12 3
A WebpackDistributionFolderExtension() 0 4 1
A WebpackFileHash() 0 14 3
1
<?php
2
3
4
class WebpackPageControllerExtension extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
7
    /**
8
     * you only need to set this if you have some themes that are enabled and some themes
9
     * that do not run webpack
10
     * @var {Array}
11
     */
12
    private static $webpack_enabled_themes = [];
0 ignored issues
show
Unused Code introduced by
The property $webpack_enabled_themes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
14
    /**
15
     * override webpack server for custom set ups
16
     * set to true to make this class believe you are always running
17
     * the webpack server
18
     * @see IsWebpackDevServer
19
     * @var bool
20
     */
21
    private static $is_webpack_server = false;
0 ignored issues
show
Unused Code introduced by
The property $is_webpack_server is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    /**
24
     * override webpack server for custom set ups
25
     * this is the server used for checking if the webpack server is running
26
     * @see IsWebpackDevServer
27
     * @var bool
28
     */
29
    private static $webpack_socket_server = 'localhost';
0 ignored issues
show
Unused Code introduced by
The property $webpack_socket_server is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    /**
32
     * usually this is set to current domain
33
     * only set if you need an alternative
34
     * @see: WebpackBaseURL
35
     * @var string
36
     */
37
    private static $webpack_server = '';
0 ignored issues
show
Unused Code introduced by
The property $webpack_server is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
38
39
    /**
40
     *
41
     * @var int
42
     */
43
    private static $webpack_port = 3000;
0 ignored issues
show
Unused Code introduced by
The property $webpack_port is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
44
45
    /**
46
     * this is the folder where the distilled files are placed.
47
     * If your theme is foo then you will find the distilled files in themes/foo_dist
48
     * @var string
49
     */
50
    private static $webpack_distribution_folder_extension = 'dist';
0 ignored issues
show
Unused Code introduced by
The property $webpack_distribution_folder_extension is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
51
52
    /**
53
     *
54
     * @return bool
55
     */
56
    public function IsNotWebpackDevServer()
57
    {
58
        return $this->IsWebpackDevServer() ? false : true;
59
    }
60
61
62
    /**
63
     *
64
     * @return bool
65
     */
66
    public function IsWebpackDevServer()
67
    {
68
        $override = $this->owner->Config()->get('is_webpack_server');
69
        if ($override) {
70
            return $override;
71
        }
72
        if (Director::isDev()) {
73
            $socket = @fsockopen(
74
                $this->owner->Config()->get('webpack_socket_server'),
75
                $this->owner->Config()->get('webpack_port'),
76
                $errno,
77
                $errstr,
78
                1
79
            );
80
            return ! $socket ? false : true;
81
        }
82
    }
83
84
85
    /**
86
     *
87
     * @return string
88
     */
89
    public function WebpackBaseURL()
90
    {
91
        $webpackServer = $this->owner->Config()->get('webpack_server');
92
        if (! $webpackServer) {
93
            $webpackServer = Director::AbsoluteBaseURL('/');
0 ignored issues
show
Unused Code introduced by
The call to Director::absoluteBaseURL() has too many arguments starting with '/'.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
94
        }
95
        if ($this->IsWebpackDevServer()) {
96
            $webpackServer = rtrim($webpackServer, '/') .':'.$this->owner->Config()->get('webpack_port').'/';
97
        }
98
99
        return $webpackServer;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function WebpackDistributionFolderExtension()
106
    {
107
        return $this->owner->Config()->get('webpack_distribution_folder_extension');
108
    }
109
110
    /**
111
     *
112
     * @param string $type should be set to JS or CSS
113
     * @return string
114
     */
115
    public function WebpackFileHash($type = 'JS')
116
    {
117
        $base = Director::baseFolder();
118
        if ($type === 'JS') {
119
            $file = 'bundle.js';
120
        } elseif ($type === 'CSS') {
121
            $file = 'style.css';
122
        } else {
123
            user_error('Please specify JS or CSS, '.$type.' specified.');
124
        }
125
        $fullFile = $base.'/'.THEMES_DIR . "/" . Config::inst()->get('SSViewer', 'theme').'_'.$this->WebpackDistributionFolderExtension().'/'.$file;
0 ignored issues
show
Bug introduced by
The variable $file does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
126
127
        return @filemtime($fullFile);
128
    }
129
}
130