Client   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 5.66 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 3
loc 53
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
C execute() 3 34 7

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
 * @author    jan huang <[email protected]>
4
 * @copyright 2017
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Console;
11
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Class Client.
20
 */
21
class Client extends Command
22
{
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('client')
27
            ->addArgument('uri', InputArgument::REQUIRED)
28
            ->addOption('data', '-d', InputOption::VALUE_OPTIONAL, 'send data')
29
            ->addOption('json', null, InputOption::VALUE_NONE, 'output json format')
30
        ;
31
    }
32
33
    /**
34
     * @param InputInterface  $input
35
     * @param OutputInterface $output
36
     *
37
     * @return mixed
38
     */
39
    public function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $uri = $input->getArgument('uri');
42
43
        $data = $input->getParameterOption(['--data', '-d']);
44
        if (file_exists($data)) {
45
            $data = file_get_contents($data);
46
        }
47
48
        client()->createRequest($uri);
49
50
        if (false === strpos(client()->getProtocol(), 'http') && empty($data)) {
51
            $data = ' ';
52
        }
53
54
        $response = client()->send($data);
55
56
        if ($input->hasParameterOption(['--json'])) {
57
            $json = $response->getContents();
58
            $header = '';
59 View Code Duplication
            foreach ($response->getHeaders() as $key => $value) {
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...
60
                $header .= $key.': '.$response->getHeaderLine($key).PHP_EOL;
61
            }
62
            $response = $header."\r\n".json_encode(json_decode($json, true), JSON_PRETTY_PRINT);
63
        } elseif (false !== strpos(client()->getProtocol(), 'http')) {
64
            $response = (string) $response;
65
        } else {
66
            $response = $response->getContents();
67
        }
68
69
        $output->writeln($response);
70
71
        return 0;
72
    }
73
}
74