Completed
Push — optimize ( 1cb82b...432add )
by Arnaud
02:20 queued 14s
created

OptimizeHtml::process()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 64

Duplication

Lines 64
Ratio 100 %

Importance

Changes 0
Metric Value
dl 64
loc 64
rs 8.1632
c 0
b 0
f 0
cc 6
nc 11
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 WyriHaximus\HtmlCompress\Factory;
12
13
/**
14
 * HTML files Optimization.
15
 */
16
class OptimizeHtml extends AbstractStepOptimize
17
{
18
    public function setProcessor()
19
    {
20
        $this->type = 'html';
21
        $this->processor = Factory::construct();
22
    }
23
24
    public function processFile(\Symfony\Component\Finder\SplFileInfo $file)
25
    {
26
        $html = file_get_contents($file->getPathname());
27
        $htmlCompressed = $this->processor->compress($html);
0 ignored issues
show
Bug introduced by
The method compress does only exist in WyriHaximus\HtmlCompress\Parser, but not in Spatie\ImageOptimizer\OptimizerChain.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
28
        \Cecil\Util::getFS()->dumpFile($file->getPathname(), $htmlCompressed);
29
    }
30
}
31