Completed
Push — master ( b25589...ff8b36 )
by Benjamin
01:02
created

Generate   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 14.94 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 4
dl 13
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 8 8 1
B execute() 5 39 6
A format() 0 16 5
A ascii() 0 9 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 Bdelespierre\PhpPhash\Command;
4
5
use Bdelespierre\PhpPhash\PHash;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class Generate extends Command
13
{
14
    protected $phash;
15
16
    public function __construct(PHash $phash)
17
    {
18
        $this->phash = $phash;
19
20
        parent::__construct();
21
    }
22
23 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method 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...
24
    {
25
        $this->setName('generate')
26
            ->setDescription('Generates the pHash of given image')
27
            ->addArgument('file', InputArgument::REQUIRED, 'Pass the file.')
28
            ->addOption('size', 's', InputOption::VALUE_REQUIRED, 'Sampling size.', 8)
29
            ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format [hex,bin,ascii].', 'hex');
30
    }
31
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $file = $input->getArgument('file');
35
        $size = $input->getOption('size');
36
37
        if (! is_readable($file)) {
38
            $output->writeln("<error>File {$file} not found or unreadable</error>");
39
40
            return Command::FAILURE;
41
        }
42
43
        if ($size < 8) {
44
            $output->writeln("<error>Sampling size must be greater or equal to 8</error>");
45
46
            return Command::FAILURE;
47
        }
48
49 View Code Duplication
        if ($size ** 2 > PHP_INT_SIZE * 8 && ! function_exists('gmp_init')) {
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...
50
            $output->writeln("<error>Sampling size too large: reduce it or install PHP-GMP extension</error>");
51
52
            return Command::FAILURE;
53
        }
54
55
        if (! in_array($input->getOption('format'), ['hex', 'bin', 'ascii'])) {
56
            $output->writeln("<error>Invalid format</error>");
57
58
            return Command::FAILURE;
59
        }
60
61
        $bits = $this->phash->hash(new \SplFileInfo($file), $size);
62
63
        $output->writeln($this->format(
64
            $bits,
65
            $input->getOption('format'),
66
            $input->getOption('size')
67
        ));
68
69
        return Command::SUCCESS;
70
    }
71
72
    protected function format(string $bits, string $format, int $size): string
73
    {
74
        switch ($format) {
75
            default:
76
            case 'bin':
77
                return $bits;
78
79
            case 'hex':
80
                return strlen($bits) <= (PHP_INT_SIZE * 8)
81
                    ? base_convert($bits, 2, 16)
82
                    : gmp_strval(gmp_init($bits, 2), 16);
83
84
            case 'ascii':
85
                return $this->ascii($bits, $size);
86
        }
87
    }
88
89
    protected function ascii(string $bits, int $size): string
90
    {
91
        return implode("\n", array_map(
92
            function ($str) {
93
                return preg_replace('/(1+)/', '<fg=green>$1</>', $str);
94
            },
95
            str_split($bits, $size)
96
        ));
97
    }
98
}
99