Completed
Pull Request — develop (#259)
by ANTHONIUS
09:01
created

Util::validateDate()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 8.8571
cc 5
eloc 10
nc 5
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
50
    /**
51
     * Convert date formatted string into a DateTime object
52
     *
53
     * @param   string  $value
54
     * @return  \DateTime|string
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\DateTime|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
     */
56
    static public function validateDate($value)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
57
    {
58
        if ($value instanceof \SolrObject || is_array($value)){
59
            return $value;
60
        }
61
        $value = trim($value);
62
        $date = \DateTime::createFromFormat(Manager::SOLR_DATE_FORMAT,$value);
63
        $check = $date && ($date->format(Manager::SOLR_DATE_FORMAT) === $value);
64
        if($check){
65
            return $date;
66
        }else{
67
            return $value;
68
        }
69
    }
70
}