Completed
Push — develop ( 143852...728aa6 )
by
unknown
08:15
created

Util::convertDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
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
14
/**
15
 * Static utility to do conversion
16
 *
17
 * @author Anthonius Munthi <[email protected]>
18
 * @since  0.26
19
 * @package Solr\Bridge
20
 */
21
class Util
22
{
23
    /**
24
     * Convert Location coordinate into acceptable solr document format
25
     * @param Location $location
26
     * @return string
27
     */
28
    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...
29
    {
30
        $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...
31
        $coordinate = doubleval($coordinates[0]).'%'.doubleval($coordinates[1]);
32
        $coordinate = strtr($coordinate,[
33
            '%'=>',',
34
            ','=>'.'
35
        ]);
36
        return $coordinate;
37
    }
38
39
    /**
40
     * Convert date time into acceptable solr document format
41
     * 
42
     * @param \DateTime $date
43
     * @return string
44
     */
45
    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...
46
    {
47
        return $date->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT);
48
    }
49
}