GeocodeCommand::humanize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the BazingaGeocoderBundle package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Bazinga\GeocoderBundle\Command;
14
15
use Geocoder\ProviderAggregator;
16
use Geocoder\Query\GeocodeQuery;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * @author Markus Bachmann <[email protected]>
25
 */
26
class GeocodeCommand extends Command
27
{
28
    protected static $defaultName = 'geocoder:geocode';
29
30
    /**
31
     * @var ProviderAggregator
32
     */
33
    private $geocoder;
34
35 1
    public function __construct(ProviderAggregator $geocoder)
36
    {
37 1
        $this->geocoder = $geocoder;
38
39 1
        parent::__construct();
40 1
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    protected function configure()
46
    {
47
        $this
48 1
            ->setName('geocoder:geocode')
49 1
            ->setDescription('Geocode an address or a ip address')
50 1
            ->addArgument('address', InputArgument::REQUIRED, 'The address')
51 1
            ->addOption('provider', null, InputOption::VALUE_OPTIONAL)
52 1
            ->setHelp(<<<'HELP'
53 1
The <info>geocoder:geocoder</info> command will fetch the latitude
54
and longitude from the given address.
55
56
You can force a provider with the "provider" option.
57
58
<info>php bin/console geocoder:geocoder "Eiffel Tower" --provider=yahoo</info>
59
HELP
60
            );
61 1
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68 1
        if ($input->getOption('provider')) {
69
            $this->geocoder->using($input->getOption('provider'));
70
        }
71
72 1
        $results = $this->geocoder->geocodeQuery(GeocodeQuery::create($input->getArgument('address')));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('address') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Geocoder\Query\GeocodeQuery::create() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
73 1
        $data = $results->first()->toArray();
74
75 1
        $max = 0;
76
77 1
        foreach ($data as $key => $value) {
78 1
            $length = strlen($key);
79 1
            if ($max < $length) {
80 1
                $max = $length;
81
            }
82
        }
83
84 1
        $max += 2;
85
86 1
        foreach ($data as $key => $value) {
87 1
            $key = $this->humanize($key);
88
89 1
            $output->writeln(sprintf(
90 1
                '<comment>%s</comment>: %s',
91 1
                str_pad($key, $max, ' ', STR_PAD_RIGHT),
92 1
                is_array($value) ? json_encode($value) : $value
93
            ));
94
        }
95
96 1
        return 0;
97
    }
98
99 1
    private function humanize(string $text): string
100
    {
101 1
        $text = preg_replace('/([A-Z][a-z]+)|([A-Z][A-Z]+)|([^A-Za-z ]+)/', ' \1', $text);
102
103 1
        return ucfirst(strtolower($text));
104
    }
105
}
106