GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 6c3a27...fa66ab )
by Askupa
02:51
created

DynamicCSSCompiler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 10
Bugs 1 Features 5
Metric Value
wmc 17
c 10
b 1
f 5
lcom 2
cbo 0
dl 0
loc 186
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A get_instance() 0 8 2
A wp_enqueue_style() 0 9 2
A compile_printed_styles() 0 13 2
A compile_external_styles() 0 10 1
A enqueue_style() 0 9 1
A register_callback() 0 4 1
A compile_styles() 0 11 3
A minify_css() 0 4 1
A filter_print() 0 4 1
A filter_external() 0 4 1
A compile_css() 0 11 2
1
<?php
2
/**
3
 * @package   WordPress Dynamic CSS
4
 * @version   1.0.4
5
 * @author    Askupa Software <[email protected]>
6
 * @link      https://github.com/askupasoftware/wp-dynamic-css
7
 * @copyright 2016 Askupa Software
8
 */
9
10
/**
11
 * Dynamic CSS Compiler Utility Class
12
 * 
13
 * 
14
 * Dynamic CSS Syntax
15
 * ------------------
16
 * <pre>
17
 * body {color: $body_color;} 
18
 * </pre>
19
 * In the above example, the variable $body_color is replaced by a value 
20
 * retrieved by the value callback function. The function is passed the variable 
21
 * name without the dollar sign, which can be used with get_option() or 
22
 * get_theme_mod() etc.
23
 */
24
class DynamicCSSCompiler
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...
25
{
26
    /**
27
     * @var Singleton The reference to *Singleton* instance of this class
28
     */
29
    private static $instance;
30
    
31
    /**
32
     * @var array The list of dynamic styles paths to compile
33
     */
34
    private $stylesheets = array();
35
    
36
    /**
37
     * @var array 
38
     */
39
    private $callbacks = array();
40
    
41
    /**
42
     * Returns the *Singleton* instance of this class.
43
     *
44
     * @return Singleton The *Singleton* instance.
45
     */
46
    public static function get_instance()
47
    {
48
        if (null === static::$instance) 
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
49
        {
50
            static::$instance = new static();
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Documentation Bug introduced by
It seems like new static() of type this<DynamicCSSCompiler> is incompatible with the declared type object<Singleton> of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
        }
52
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Bug Compatibility introduced by
The expression static::$instance; of type DynamicCSSCompiler|Singleton adds the type DynamicCSSCompiler to the return on line 52 which is incompatible with the return type documented by DynamicCSSCompiler::get_instance of type Singleton.
Loading history...
53
    }
54
    
55
    /**
56
     * Enqueue the PHP script used for compiling dynamic stylesheets that are 
57
     * loaded externally
58
     */
59
    public function wp_enqueue_style()
60
    {
61
        // Only enqueue if there is at least one dynamic stylesheet that is
62
        // set to be loaded externally
63
        if( 0 < count( array_filter($this->stylesheets, array( $this, 'filter_external' ) ) ) )
64
        {
65
            wp_enqueue_style( 'wp-dynamic-css', admin_url( 'admin-ajax.php?action=wp_dynamic_css' ) );
66
        }
67
    }
68
    
69
    /**
70
     * Parse all styles in $this->stylesheets and print them if the flag 'print'
71
     * is set to true. Used for printing styles to the document head.
72
     */
73
    public function compile_printed_styles()
74
    {
75
        $styles = array_filter($this->stylesheets, array( $this, 'filter_print' ) );
76
        
77
        // Bail if there are no styles to be printed
78
        if( count( $styles ) === 0 ) return;
79
        
80
        $compiled_css = $this->compile_styles( $styles );
81
        
82
        echo "<style id=\"wp-dynamic-css\">\n";
83
        include 'style.phtml';
84
        echo "</style>";
85
    }
86
    
87
    /**
88
     * Parse all styles in $this->stylesheets and print them if the flag 'print'
89
     * is not set to true. Used for loading styles externally via an http request.
90
     */
91
    public function compile_external_styles()
92
    {
93
        header( "Content-type: text/css; charset: UTF-8" );
94
        header( "Cache-Control: no-cache, must-revalidate" ); //set headers to NOT cache so that changes to options are reflected immediately
95
        
96
        $compiled_css = $this->compile_styles( array_filter($this->stylesheets, array( $this, 'filter_external' ) ) );
97
        
98
        include 'style.phtml';
99
        wp_die();
100
    }
101
    
102
    /**
103
     * Add a style path to the pool of styles to be compiled
104
     * 
105
     * @param string $handle The stylesheet's name/id
106
     * @param string $path The absolute path to the dynamic style
107
     * @param boolean $print Whether to print the compiled CSS to the document
108
     * head, or include it as an external CSS file
109
     * @param boolean $minify Whether to minify the CSS output
110
     */
111
    public function enqueue_style( $handle, $path, $print, $minify )
112
    {
113
        $this->stylesheets[] = array(
114
            'handle'=> $handle,
115
            'path'  => $path,
116
            'print' => $print,
117
            'minify'=> $minify
118
        );
119
    }
120
    
121
    /**
122
     * Register a value retrieval function and associate it with the given handle
123
     * 
124
     * @param type $handle The stylesheet's name/id
125
     * @param type $callback
126
     */
127
    public function register_callback( $handle, $callback )
128
    {
129
        $this->callbacks[$handle] = $callback;
130
    }
131
132
    /**
133
     * Compile multiple dynamic stylesheets
134
     * 
135
     * @param array $styles List of styles with the same structure as they are 
136
     * stored in $this->stylesheets
137
     * @return string Compiled CSS
138
     */
139
    protected function compile_styles( $styles )
140
    {
141
        $compiled_css = '';
142
        foreach( $styles as $style ) 
143
        {
144
            $css = file_get_contents( $style['path'] );
145
            if( $style['minify'] ) $css = $this->minify_css ( $css );
146
            $compiled_css .= $this->compile_css( $css, $this->callbacks[$style['handle']] )."\n";
147
        }
148
        return $compiled_css;
149
    }
150
    
151
    /**
152
     * Minify a given CSS string by removing comments, whitespaces and newlines
153
     * 
154
     * @see http://stackoverflow.com/a/6630103/1096470
155
     * @param string $css CSS style to minify
156
     * @return string Minified CSS
157
     */
158
    protected function minify_css( $css )
159
    {
160
        return preg_replace( '@({)\s+|(\;)\s+|/\*.+?\*\/|\R@is', '$1$2 ', $css );
161
    }
162
163
    /**
164
     * This filter is used to return only the styles that are set to be printed
165
     * in the document head
166
     * 
167
     * @param array $style
168
     * @return boolean
169
     */
170
    protected function filter_print( $style )
171
    {
172
        return true === $style['print'];
173
    }
174
    
175
    /**
176
     * This filter is used to return only the styles that are set to be loaded
177
     * externally
178
     * 
179
     * @param array $style
180
     * @return boolean
181
     */
182
    protected function filter_external( $style )
183
    {
184
        return true !== $style['print'];
185
    }
186
    
187
    /**
188
     * Parse the given CSS string by converting the variables to their 
189
     * corresponding values retrieved by applying the callback function
190
     * 
191
     * @param callable $callback A function that replaces the variables with 
192
     * their values. The function accepts the variable's name as a parameter
193
     * @param string $css A string containing dynamic CSS (pre-compiled CSS with 
194
     * variables)
195
     * @return string The compiled CSS after converting the variables to their 
196
     * corresponding values
197
     */
198
    protected function compile_css( $css, $callback )
199
    {   
200
        return preg_replace_callback( "#\\$([\\w-]+)((?:\\['?[\\w-]+'?\\])*)#", function( $matches ) use ( $callback ) {
201
            // If this variable is an array, get the subscripts
202
            if( '' !== $matches[2] )
203
            {
204
                preg_match_all('/[\w-]+/i', $matches[2], $subscripts);
205
            }
206
            return call_user_func_array( $callback, array($matches[1],@$subscripts[0]) );
0 ignored issues
show
Bug introduced by
The variable $subscripts 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...
207
        }, $css);
208
    }
209
}
210