Passed
Push — master ( 25ef95...88bd2b )
by Stephen
02:06
created

ExtractCityStateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 16 4
1
<?php
2
3
4
namespace Sfneal\GooglePlaces;
5
6
7
use Sfneal\Actions\AbstractActionStatic;
8
use Sfneal\GooglePlaces\Actions\Traits\States;
9
10
class ExtractCityStateAction extends AbstractActionStatic
11
{
12
    use States;
13
14
    /**
15
     * Extract a (city, state) string from a google places API return that may contain a zip code
16
     *
17
     * @param string $description
18
     * @return array
19
     */
20
    public static function execute(string $description)
21
    {
22
        // Check that the place description contains a comma string
23
        if (strpos($description, ', ') !== false) {
24
            // Separate city and state strings
25
            list($city, $state) = explode(', ', $description);
26
27
            // Check if the state string contains more than a state code
28
            if (strlen(trim($state)) > 2 && strpos($state, ' ') !== false) {
29
                // Extract the zip code
30
                list($state, $zip) = explode(' ', $state);
31
            }
32
            return [$city, $state];
33
34
        } else {
35
            return ['', self::stateAbbreviation($description)];
36
        }
37
    }
38
}
39