FindText::findText()   C
last analyzed

Complexity

Conditions 13
Paths 98

Size

Total Lines 58
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 13

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 58
ccs 24
cts 24
cp 1
rs 6.6166
cc 13
nc 98
nop 9
crap 13

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace kalanis\google_maps\Services;
4
5
6
use kalanis\google_maps\ServiceException;
7
use Psr\Http\Message\RequestInterface;
8
9
10
/**
11
 * Find by text service
12
 *
13
 * @see     https://developers.google.com/maps/documentation/places/web-service/search-text
14
 */
15
class FindText extends AbstractService
16
{
17
    /**
18
     * Find by text lookup
19
     *
20
     * @param string $query what will be searched
21
     * @param float $radius in which radius
22
     * @param array<string|int, float> $location ['lat', 'lng', 'rad']
23
     * @param int<0,4>|null $maxPrice
24
     * @param int<0,4>|null $minPrice
25
     * @param bool $openNow
26
     * @param string|null $region
27
     * @param string|null $type
28
     * @param array<string, string|int|float> $params Query parameters
29
     * @throws ServiceException
30
     * @return RequestInterface
31
     */
32 5
    public function findText(
33
        string  $query,
34
        float   $radius,
35
        array   $location = [],
36
        ?int    $maxPrice = null,
37
        ?int    $minPrice = null,
38
        bool    $openNow = false,
39
        ?string $region = null,
40
        ?string $type = null,
41
        array   $params = []
42
    ): RequestInterface
43
    {
44 5
        if (empty($query) || empty($radius)) {
45 1
            throw new ServiceException('You must set where to look!');
46
        }
47
48
        // Main wanted name
49 4
        $params['query'] = $query;
50 4
        $params['radius'] = sprintf('%1.02F', $radius);
51
52
        // `location` seems to only allow `lat,lng` pattern
53 4
        if (!empty($location)) {
54
55 3
            if (isset($location['lat']) && isset($location['lng'])) {
56 1
                $params['location'] = sprintf('%1.08F,%1.08F', $location['lat'], $location['lng']);
57
58 2
            } elseif (isset($location[0]) && isset($location[1])) {
59 1
                $params['location'] = sprintf('%1.08F,%1.08F', $location[0], $location[1]);
60
61
            } else {
62 1
                throw new ServiceException('Passed invalid values into coordinates! You must use either array with lat and lng and rad or 0, 1, 2 and 3 keys.');
63
64
            }
65
        }
66
67 3
        if (!empty($maxPrice)) {
68 1
            $params['maxprice'] = $maxPrice;
69
        }
70
71 3
        if (!empty($minPrice)) {
72 1
            $params['minprice'] = $minPrice;
73
        }
74
75 3
        if ($openNow) {
76 1
            $params['opennow'] = 'true';
77
        }
78
79 3
        if (!empty($region)) {
80 1
            $params['region'] = strtolower(substr($region, 0, 2));
81
        }
82
83 3
        if (!empty($type)) {
84 1
            $params['type'] = $type;
85
        }
86
87 3
        return $this->getWithDefaults(
88 3
            static::API_HOST . '/maps/api/place/textsearch/json',
89 3
            $this->queryParamsLang($params)
90 3
        );
91
    }
92
}
93