SizeRule   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 2
dl 20
loc 20
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 4 4 1
A doValidation() 5 5 1

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
namespace whm\Smoke\Rules\Image;
4
5
use Psr\Http\Message\ResponseInterface;
6
use whm\Smoke\Rules\StandardRule;
7
8
/**
9
 * This rule checks if the size of an image is bigger than a given max value.
10
 */
11 View Code Duplication
class SizeRule extends StandardRule
12
{
13
    private $maxSize;
14
15
    protected $contentTypes = array('image');
16
17
    /**
18
     * @param int $maxSize The maximum size of an image file in kilobytes
19
     */
20
    public function init($maxSize = 100)
21
    {
22
        $this->maxSize = $maxSize;
23
    }
24
25
    protected function doValidation(ResponseInterface $response)
26
    {
27
        $size = strlen((string)$response->getBody()) / 1000;
28
        $this->assert($size <= $this->maxSize, 'The size of the file is too big (' . $size . ' KB)');
29
    }
30
}
31