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 ( e06a5d...2982a9 )
by Askupa
02:12
created

DynamicCSSCompiler::get_file_contents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * @package   WordPress Dynamic CSS
4
 * @version   1.0.2
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
        $compiled_css = '';
76
        $styles = array_filter($this->stylesheets, array( $this, 'filter_print' ) );
77
        
78
        // Bail if there are no styles to be printed
79
        if( count( $styles ) === 0 ) return;
80
        
81 View Code Duplication
        foreach( $styles as $style ) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
        {
83
            $css = file_get_contents( $style['path'] );
84
            $compiled_css .= $this->compile_css( $css, $this->callbacks[$style['handle']] )."\n";
85
        }
86
        
87
        echo "<style id=\"wp-dynamic-css\">\n";
88
        include 'style.phtml';
89
        echo "</style>";
90
    }
91
    
92
    /**
93
     * Parse all styles in $this->stylesheets and print them if the flag 'print'
94
     * is not set to true. Used for loading styles externally via an http request.
95
     */
96
    public function compile_external_styles()
97
    {
98
        header( "Content-type: text/css; charset: UTF-8" );
99
        $compiled_css = '';
100
        $styles = array_filter($this->stylesheets, array( $this, 'filter_external' ) );
101
        
102 View Code Duplication
        foreach( $styles as $style ) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
        {
104
            $css = file_get_contents( $style['path'] );
105
            $compiled_css .= $this->compile_css( $css, $this->callbacks[$style['handle']] )."\n";
106
        }
107
        
108
        include 'style.phtml';
109
        wp_die();
110
    }
111
    
112
    /**
113
     * Add a style path to the pool of styles to be compiled
114
     * 
115
     * @param string $handle The stylesheet's name/id
116
     * @param string $path The absolute path to the dynamic style
117
     * @param boolean $print Whether to print the compiled CSS to the document 
118
     * head, or include it as an external CSS file
119
     */
120
    public function enqueue_style( $handle, $path, $print )
121
    {
122
        $this->stylesheets[] = array(
123
            'handle'=> $handle,
124
            'path'  => $path,
125
            'print' => $print
126
        );
127
    }
128
    
129
    /**
130
     * Register a value retrieval function and associate it with the given handle
131
     * 
132
     * @param type $handle The stylesheet's name/id
133
     * @param type $callback
134
     */
135
    public function register_callback( $handle, $callback )
136
    {
137
        $this->callbacks[$handle] = $callback;
138
    }
139
140
    /**
141
     * This filter is used to return only the styles that are set to be printed
142
     * in the document head
143
     * 
144
     * @param array $style
145
     * @return boolean
146
     */
147
    protected function filter_print( $style )
148
    {
149
        return true === $style['print'];
150
    }
151
    
152
    /**
153
     * This filter is used to return only the styles that are set to be loaded
154
     * externally
155
     * 
156
     * @param array $style
157
     * @return boolean
158
     */
159
    protected function filter_external( $style )
160
    {
161
        return true !== $style['print'];
162
    }
163
    
164
    /**
165
     * Parse the given CSS string by converting the variables to their 
166
     * corresponding values retrieved by applying the callback function
167
     * 
168
     * @param callable $callback A function that replaces the variables with 
169
     * their values. The function accepts the variable's name as a parameter
170
     * @param string $css A string containing dynamic CSS (pre-compiled CSS with 
171
     * variables)
172
     * @return string The compiled CSS after converting the variables to their 
173
     * corresponding values
174
     */
175
    protected function compile_css( $css, $callback )
176
    {   
177
        return preg_replace_callback( '#\$([\w]+)#', function( $matches ) use ( $callback ) {
178
            return call_user_func_array( $callback, array($matches[1]) );
179
        }, $css);
180
    }
181
}
182