Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Statics   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 10 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 4
loc 40
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
D file() 4 28 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Inji;
3
/**
4
 * Statics
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Statics {
12
13
    /**
14
     * Cached static file and return absolute url for client side use
15
     *
16
     * @param string $path
17
     * @param string $resize
18
     * @param string $resizeCrop
19
     * @param string $resizePos
20
     * @return string
21
     */
22
    public static function file($path, $resize = '', $resizeCrop = '', $resizePos = '', $pathAbsolute = false) {
23
        if (!$path) {
24
            $pathAbsolute = false;
25
            $path = !empty(App::$primary->config['site']['noimage']) ? App::$primary->config['site']['noimage'] : '/static/system/images/no-image.png';
26
        }
27
28
        $absolutePath = $pathAbsolute ? $path : App::$cur->staticLoader->parsePath($path);
29
30
        $convert = false;
31 View Code Duplication
        if (!file_exists($absolutePath) && file_exists(mb_convert_encoding($absolutePath, 'Windows-1251', 'UTF-8'))) {
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...
32
            $absolutePath = mb_convert_encoding($absolutePath, 'Windows-1251', 'UTF-8');
33
            $convert = true;
34
        }
35
        if (!file_exists($absolutePath)) {
36
            return '';
37
        } else {
38
            $options = [];
39
            if ($resize) {
40
                $resize = explode('x', $resize);
41
                $options = ['resize' => ['x' => $resize[0], 'y' => $resize[1]]];
42
            }
43
            $options['crop'] = $resizeCrop;
44
            $options['pos'] = $resizePos;
45
            $path = Cache::file($absolutePath, $options);
46
            $path = $convert ? mb_convert_encoding($path, 'UTF-8', 'Windows-1251') : $path;
47
            return '/' . $path;
48
        }
49
    }
50
}