Completed
Pull Request — develop (#284)
by
unknown
09:13
created

Util::convertLocationCoordinates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Solr\Bridge;
11
12
use Jobs\Entity\Location;
13
use DateTime;
14
15
/**
16
 * Static utility to do conversion
17
 *
18
 * @author Anthonius Munthi <[email protected]>
19
 * @author Miroslav Fedeleš <[email protected]>
20
 * @since  0.26
21
 * @package Solr\Bridge
22
 */
23
class Util
24
{
25
    /**
26
     * Convert Location coordinate into acceptable solr document format
27
     * @param Location $location
28
     * @return string
29
     */
30
    static public function convertLocationCoordinates(Location $location)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
31
    {
32
        $coordinates = $location->getCoordinates()->getCoordinates();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class GeoJson\GeoJson as the method getCoordinates() does only exist in the following sub-classes of GeoJson\GeoJson: GeoJson\Geometry\Geometry, GeoJson\Geometry\GeometryCollection, GeoJson\Geometry\LineString, GeoJson\Geometry\LinearRing, GeoJson\Geometry\MultiLineString, GeoJson\Geometry\MultiPoint, GeoJson\Geometry\MultiPolygon, GeoJson\Geometry\Point, GeoJson\Geometry\Polygon, Geo\Entity\Geometry\Point. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
33
        $coordinate = doubleval($coordinates[0]).'%'.doubleval($coordinates[1]);
34
        $coordinate = strtr($coordinate,[
35
            '%'=>',',
36
            ','=>'.'
37
        ]);
38
        return $coordinate;
39
    }
40
41
    /**
42
     * Convert date time into acceptable solr document format
43
     *
44
     * @param DateTime $date
45
     * @return string
46
     */
47
    static public function convertDateTime(DateTime $date)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
48
    {
49
        return $date->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT);
50
    }
51
52
    /**
53
     * Convert Solr date into a PHP DateTime object
54
     *
55
     * @param string $solrDate
56
     * @return DateTime
57
     */
58
    static public function convertSolrDateToPhpDateTime($solrDate)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
59
    {
60
        $solrDate = trim($solrDate);
61
        $dateTime = DateTime::createFromFormat(Manager::SOLR_DATE_FORMAT, $solrDate);
62
        $valid = $dateTime && ($dateTime->format(Manager::SOLR_DATE_FORMAT) === $solrDate);
63
        
64
        if (!$valid) {
65
            throw new \InvalidArgumentException(sprintf('invalid format of Solr date passed: "%s"', $solrDate));
66
        }
67
        
68
        return $dateTime;
69
    }
70
}