PHash   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 30.3 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B hash() 10 23 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
namespace Bdelespierre\PhpPhash;
4
5
use Intervention\Image\ImageManager;
6
7
class PHash
8
{
9
    protected $manager;
10
11
    public function __construct(ImageManager $manager)
12
    {
13
        $this->manager = $manager;
14
    }
15
16
    public function hash(\SplFileInfo $file, int $size = 8): string
17
    {
18
        $image = $this->manager->make($file)
19
            ->resize($size, $size)
20
            ->greyscale();
21
22
        $sum = 0;
23 View Code Duplication
        for ($x = 0; $x < $size; ++$x) {
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...
24
            for ($y = 0; $y < $size; ++$y) {
25
                $sum += $image->pickColor($x, $y, 'array')[0];
26
            }
27
        }
28
29
        $mean = $sum / ($size ** 2);
30
        $bits = '';
31 View Code Duplication
        for ($x = 0; $x < $size; ++$x) {
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
            for ($y = 0; $y < $size; ++$y) {
33
                $bits .= $image->pickColor($x, $y, 'array')[0] > $mean ? 1 : 0;
34
            }
35
        }
36
37
        return $bits;
38
    }
39
}
40