Module::compressCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: egorov
5
 * Date: 11.01.2015
6
 * Time: 9:18
7
 */
8
9
namespace samsonos\compressor;
10
11
use samsonphp\compressor\Code;
12
use samsonphp\compressor\Compressor;
13
14
/**
15
 * Class for automatic SamsonPHP module optimization|compression
16
 *
17
 * @package samsonos\compressor
18
 * @author Vitaly Iegorov <[email protected]>
19
 */
20
class Module
21
{
22
    /** @var \samson\core\Core Core pointer */
23
    protected $core;
24
25
    /** @var \samson\core\CompressableExternalModule Module pointer */
26
    protected $module;
27
28
    /** @var  Object Logger object */
29
    protected $logger;
30
31
    /** @var  array Collection of module PHP code */
32
    protected $code = array();
33
34
    /** @var array Ignored resource extensions */
35
    public $ignoredExtensions = array(
36
        'php', 'js', 'css', 'md', 'map', 'dbs', 'vphp', 'less' , 'gz', 'lock', 'json', 'sql', 'xml', 'yml'
37
    );
38
39
    /**
40
     * @param \samson\core\Core     $core Core pointer
41
     * @param \samson\core\CompressableExternalModule   $module Module pointer
42
     * @param object $logger Logger object
43
     */
44
    public function __construct($core, $module, $logger = null)
45
    {
46
        $this->core = & $core;
47
        $this->module = & $module;
48
        $this->logger = & $logger;
49
    }
50
51
    /**
52
     * Perform module compression
53
     * @return bool True if module was successfully compressed
54
     */
55
    public function compress()
56
    {
57
        // Cache module ID
58
        $id = $this->module->id();
59
60
        $this->logger->log('  - Compressing module[##]', $id);
61
62
        // Call special method enabling module personal resource pre-management on compressing
63
        if ($this->module->beforeCompress($this, $this->code) !== false) {
64
            //$this->compressResources();
65
            //$this->compressView();
66
            //$this->compressTemplate();
67
68
            if (is_a($this->module->resourceMap, 'samson\core\ResourceMap')) {
69
                $sources = array_merge(
70
                    $this->module->resourceMap->php,
0 ignored issues
show
Bug introduced by
Accessing php on the interface samsonframework\core\ResourcesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
71
                    $this->module->resourceMap->controllers,
0 ignored issues
show
Bug introduced by
Accessing controllers on the interface samsonframework\core\ResourcesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
72
                    $this->module->resourceMap->models,
0 ignored issues
show
Bug introduced by
Accessing models on the interface samsonframework\core\ResourcesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
73
                    $this->module->resourceMap->globals
0 ignored issues
show
Bug introduced by
Accessing globals on the interface samsonframework\core\ResourcesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
74
                );
75
76
                // Add module class files to array of sources
77
                foreach ($this->module->resourceMap->modules as $module) {
0 ignored issues
show
Bug introduced by
Accessing modules on the interface samsonframework\core\ResourcesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
78
                    $sources = array_merge(array($module[1]), $sources);
79
                }
80
81
                // Create code collector
82
                $code = new Code($sources, $this->logger);
0 ignored issues
show
Unused Code introduced by
$code is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
            }
84
85
            // Change module path, now all modules would be located at wwwroot folder
86
            //$this->module->path($id.'/');
87
88
            // Call special method enabling module personal resource post-management on compressing
89
            //$this->module->afterCompress($this, $this->code);
90
91
            return true;
92
        }
93
94
        $this->logger->log('  - Module[##] compression stopped', $id);
95
96
        return false;
97
    }
98
99
    public function compressResources()
100
    {
101
        // Build output module path
102
        $destination = $id == 'local' ? '' : $id.'/';
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
103
104
        // Build resource source path
105
        $source = $id == 'local' ? $this->module->path().__SAMSON_PUBLIC_PATH : $this->module->path();
106
107
        $this->log(' -> Copying resources from [##] to [##]', $module_path, $module_output_path);
0 ignored issues
show
Bug introduced by
The variable $module_path does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $module_output_path does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The method log() does not seem to exist on object<samsonos\compressor\Module>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
109
        // Iterate module resources
110
        foreach ($this->module->resourceMap->resources as $extension => $resources) {
0 ignored issues
show
Bug introduced by
Accessing resources on the interface samsonframework\core\ResourcesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
111
            // Iterate only allowed resource types
112
            if (!in_array( $extension , $this->ignored_extensions)) {
0 ignored issues
show
Bug introduced by
The property ignored_extensions does not seem to exist. Did you mean ignoredExtensions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Coding Style introduced by
Space found before comma in function call
Loading history...
113
                foreach ( $resources as $resource ) {
0 ignored issues
show
Coding Style introduced by
Space found after opening bracket of FOREACH loop
Loading history...
Coding Style introduced by
Space found before closing bracket of FOREACH loop
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
114
                    // Get only filename
115
                    $filename = basename( $resource );
116
117
                    // Copy only allowed resources
118
                    if (!in_array( $filename, $this->ignored_resources)) {
0 ignored issues
show
Bug introduced by
The property ignored_resources does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
119
                        // Build relative module resource path
120
                        $relative_path = str_replace($module_path, '', $resource);
121
122
                        // Build correct destination folder
123
                        $dst = $this->output.$module_output_path.$relative_path;
0 ignored issues
show
Bug introduced by
The property output does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
124
125
                        // Copy/update file if necessary
126
                        $this->copy_resource( $resource, $dst );
0 ignored issues
show
Bug introduced by
The method copy_resource() does not seem to exist on object<samsonos\compressor\Module>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
                    }
128
                }
129
            }
130
        }
131
132
        // Copy all module resources
133
        $this->copy_path_resources($this->module->resourceMap->resources, $source, $destination);
0 ignored issues
show
Bug introduced by
Accessing resources on the interface samsonframework\core\ResourcesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
The method copy_path_resources() does not seem to exist on object<samsonos\compressor\Module>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
    }
135
136
    public function compressView()
137
    {
138
        // Iterate all views
139
        foreach ($this->module->resourceMap->views as $view) {
0 ignored issues
show
Bug introduced by
Accessing views on the interface samsonframework\core\ResourcesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
140
141
        }
142
    }
143
144
    /**
145
     * Recursively gather PHP code from file and gather
146
     * it into array, grouped by namespace
147
     */
148
    public function compressCode($file, array $code = array(), $namespace = Compressor::NS_GLOBAL)
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $code is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $namespace is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
    {
150
151
    }
152
153
    public function compressTemplate()
154
    {
155
156
    }
157
158
    /** @deprecated Use compressCode() instead */
159
    public function compress_php($file, $module, array $code = array(), $namespace = Compressor::NS_GLOBAL)
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "Module::compress_php" is not in camel caps format
Loading history...
160
    {
161
        $this->logger->log(' - Compressing PHP file[##]', $file);
162
        return $this->compressCode($file, $code, $namespace);
163
    }
164
}
165