GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DateTimeField::getData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Solvire\API\Serializers\DataFields;
3
4
use Carbon\Carbon;
5
6
/**
7
 * A DateTime object - Carbon
8
 *
9
 * It would be nicer to us if you pass in an object so we don't have to guess.
10
 * BTW Carbon Rocks
11
 *
12
 * @see http://carbon.nesbot.com/docs/
13
 *
14
 *
15
 * @author solvire <[email protected]>
16
 * @package DataFields
17
 * @namespace Solvire\API\Serializers\DataFields
18
 */
19
class DateTimeField extends DataField
20
{
21
22
    /**
23
     * default formatting of ISO8601
24
     * 
25
     * NOTE: make sure to set the formatting that will be coming in. 
26
     * It can be set in the constructor or after the fact  
27
     *
28
     * @var string [Y-m-d\TH:i:sO]
29
     */
30
    protected $format = Carbon::ISO8601;
31
32
    /**
33
     * 
34
     * @var unknown
35
     */
36
    protected $cast = 'datetime';
37
    
38
    /**
39
     * 
40
     * @param array $options
41
     */
42 10
    public function __construct($options=[])
43
    {
44
        // load up the format at creation time  
45 10
        if(isset($options['format']))
46 10
            $this->format = $options['format'];
47 10
        parent::__construct($options);
48 10
    }
49
50
    /**
51
     *
52
     *
53
     * This can take any of the three formats listed.
54
     * 
55
     * @param $data mixed
56
     *            [string, Carbon, DateTime]
57
     *            
58
     * @return $this (non-PHPdoc)
59
     * @throws RuntimeException
60
     * @see \Solvire\API\Serializers\DataFields\DataField::setData()
61
     */
62 5
    public function setData($data)
63
    {
64
        try {
65 5
            if (is_string($data)) {
66 1
                $date = Carbon::createFromFormat($this->format, $data);
67 5
            } elseif ($data instanceof Carbon) {
68 3
                $date = $data;
69 5
            } elseif ($data instanceof \DateTime) {
70 3
                $date = Carbon::instance($data);
71 3
            }
72 5
        } catch (\Exception $e) {
73 1
            throw new \RuntimeException('DateTimeField data must be a string representation of a date/time object in format: ' . $this->format, 400, $e);
74
        }
75
        
76 5
        $this->data = $date;
0 ignored issues
show
Bug introduced by
The variable $date does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
77 5
        return $this;
78
    }
79
    
80
    /**
81
     * 
82
     * @param string $format
83
     */
84 1
    public function setFormat($format)
85
    {
86 1
        $this->format = $format;
87 1
    }
88
89
    /**
90
     * This is a char so it will always be just a string
91
     */
92 5
    public function getData()
93
    {
94 5
        return $this->data;
95
    }
96
}