Completed
Push — master ( 4d2480...708604 )
by Ben
02:48
created

DateTime::output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Benrowe\Formatter\Providers;
4
5
use Benrowe\Formatter\AbstractFormatterProvider;
6
use Carbon\Carbon;
7
use Carbon\CarbonInterval;
8
9
10
/**
11
 * DateTime Formatter
12
 *
13
 * Contains formatter for datetime values
14
 *
15
 * @package Benrowe\Formatter
16
 */
17
class DateTime extends AbstractFormatterProvider
18
{
19 1
    public function asDate($value, $format = 'Y-m-d')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
    {
21 1
        return $this->output($value, $format);
22
    }
23
24 1
    public function asTime($value, $format = 'H:i:s')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
25
    {
26 1
        return $this->output($value, $format);
27
    }
28
29 1
    public function asDateTime($value, $format = 'Y-m-d H:i:s')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
30
    {
31 1
        return $this->output($value, $format);
32
    }
33
34
    /**
35
     * Take in a number of seconds and display that as a human readable amount of time
36
     *
37
     * @param  int $value
38
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

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...
39
     */
40
    public function asHuman($value)
41
    {
42
        $int = CarbonInterval::seconds($value);
0 ignored issues
show
Unused Code introduced by
$int is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
    }
44
45 3
    private function output($value, $format)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
46
    {
47 3
        return $this
48 3
            ->normaliseValue($value)
49 3
            ->format($format);
50
    }
51
52 3
    private function normaliseValue($value)
53
    {
54 3
        $carbon = $this->getCarbon();
55
        // sniff the value type
56 3
        if (is_int($value)) {
57 3
            return $carbon->createFromTimestamp($value);
58
        }
59
        return $carbon->parse($value);
60
    }
61
62
    /**
63
     * Get an instance of the carbon datetime handler
64
     *
65
     * @return Carbon
66
     */
67 3
    private function getCarbon()
68
    {
69 3
        if (!class_exists('Carbon\Carbon')) {
70
            throw new FormatterException(
71
                'Unable to load the nesbot/carbon dependency.'
72
            );
73
        }
74 3
        return new Carbon;
75
    }
76
}
77