Completed
Push — optimize ( 97f596 )
by Arnaud
02:46
created

OptimizeHtml   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 95
loc 95
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 18 18 5
B process() 64 64 6

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
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Symfony\Component\Finder\Finder;
12
use WyriHaximus\HtmlCompress\Factory;
13
14
/**
15
 * HTML files Optimization.
16
 */
17 View Code Duplication
class OptimizeHtml extends AbstractStep
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
18
{
19
    const TYPE = 'html';
20
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function init($options)
26
    {
27
        if (false === $this->builder->getConfig()->get(sprintf('optimize.%s.enabled', self::TYPE))
28
            || false === $this->builder->getConfig()->get('optimize.enabled'))
29
        {
30
            $this->process = false;
31
32
            return;
33
        }
34
        if ($options['dry-run']) {
35
            $this->process = false;
36
37
            return;
38
        }
39
        if (is_dir($this->builder->getConfig()->getOutputPath())) {
40
            $this->process = true;
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function process()
48
    {
49
        call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE', sprintf('Optimizing %s', self::TYPE)]);
50
51
        $extensions = $this->builder->getConfig()->get(sprintf('optimize.%s.ext', self::TYPE));
52
        $files = Finder::create()
53
            ->files()
54
            ->in($this->builder->getConfig()->getOutputPath())
55
            ->name('/\.('.implode('|', $extensions).')$/')
56
            ->notName('/\.min\.(' . implode('|', $extensions) . ')$/')
57
            ->sortByName(true);
58
        $max = count($files);
59
60
        if ($max <= 0) {
61
            $message = 'No files';
62
            call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]);
63
64
            return;
65
        }
66
67
        $count = 0;
68
        $optimized = 0;
69
70
        // setup processor
71
        $parser = Factory::construct();
72
73
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
74
        foreach ($files as $file) {
75
            $sizeBefore = 0;
0 ignored issues
show
Unused Code introduced by
$sizeBefore 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...
76
            $sizeAfter = 0;
0 ignored issues
show
Unused Code introduced by
$sizeAfter 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...
77
            $count++;
78
79
            $sizeBefore = $file->getSize();
80
81
            // process file
82
            $html = file_get_contents($file->getPathname());
83
            $htmlCompressed = $parser->compress($html);
84
            \Cecil\Util::getFS()->dumpFile($file->getPathname(), $htmlCompressed);
85
86
            $sizeAfter = $file->getSize();
87
88
            $subpath = \Cecil\Util::getFS()->makePathRelative(
89
                $file->getPath(),
90
                $this->builder->getConfig()->getOutputPath()
91
            );
92
            $subpath = trim($subpath, './');
93
            $path = $subpath ? $subpath.'/'.$file->getFilename() : $file->getFilename();
94
95
            $message = sprintf(
96
                '%s: %s Ko -> %s Ko',
97
                $path,
98
                ceil($sizeBefore / 1000),
99
                ceil($sizeAfter / 1000)
100
            );
101
            call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message, $count, $max]);
102
            if ($sizeAfter < $sizeBefore) {
103
                $optimized++;
104
            }
105
        }
106
        if ($optimized == 0) {
107
            $message = 'Nothing to do';
108
            call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]);
109
        }
110
    }
111
}
112