Completed
Branch master (082d55)
by Rudie
02:26
created

FuelUp   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 68
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createMileage() 0 4 1
A createFromTrend() 0 8 2
A createFromDetail() 0 5 1
A __construct() 0 15 2
A dateCmp() 0 3 1
1
<?php
2
3
namespace rdx\fuelly;
4
5
use DateTime;
6
use rdx\fuelly\Vehicle;
7
use rdx\units\Length;
8
use rdx\units\Mileage;
9
use rdx\units\Volume;
10
11
class FuelUp {
12
13
	public $vehicle; // rdx\fuelly\Vehicle
14
15
	public $date; // DateTime
16
	public $volume; // rdx\units\Volume
17
	public $distance; // rdx\units\Length
18
	public $mileage; // rdx\units\Mileage
19
20
	public $raw_volume;
21
	public $raw_distance;
22
23
	/**
24
	 *
25
	 */
26
	public static function createFromTrend( Vehicle $vehicle, array $fuelup, InputConversion $input = null ) {
27
		// Trend is always in real numbers, and only its natives are reliable so use those
28
		$input or $input = $vehicle->client->createTrendInputConversion();
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
29
30
		// @todo Parse date correctly
31
		$date = DateTime::createFromFormat('Y-m-d H:i:s', $fuelup['fuelup_date']);
32
		return new static($vehicle, $date, $fuelup['miles_last_fuelup'], $fuelup['amount'], $input);
0 ignored issues
show
Security Bug introduced by
It seems like $date defined by \DateTime::createFromFor...$fuelup['fuelup_date']) on line 31 can also be of type false; however, rdx\fuelly\FuelUp::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
33
	}
34
35
	/**
36
	 *
37
	 */
38
	public static function createFromDetail( Vehicle $vehicle, array $fuelup, InputConversion $input = null ) {
39
		// @todo Parse date correctly
40
		$date = DateTime::createFromFormat('d-m-y', $fuelup['fuelup_date']);
41
		return new static($vehicle, $date, $fuelup['miles_last_fuelup'], $fuelup['amount'], $input);
0 ignored issues
show
Security Bug introduced by
It seems like $date defined by \DateTime::createFromFor...$fuelup['fuelup_date']) on line 40 can also be of type false; however, rdx\fuelly\FuelUp::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
42
	}
43
44
	/**
45
	 *
46
	 */
47
	protected function __construct( Vehicle $vehicle, DateTime $date, $raw_distance, $raw_volume, InputConversion $input = null ) {
48
		$this->vehicle = $vehicle;
49
50
		$input or $input = $vehicle->client->input;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
51
52
		$this->date = $date;
53
54
		$this->raw_volume = $input->convertNumber($raw_volume);
55
		$this->raw_distance = $input->convertNumber($raw_distance);
56
57
		$this->volume = $input->convertVolume($this->raw_volume);
58
		$this->distance = $input->convertDistance($this->raw_distance);
59
60
		$this->mileage = static::createMileage($this->distance, $this->volume);
61
	}
62
63
	/**
64
	 *
65
	 */
66
	public static function createMileage( Length $distance, Volume $volume ) {
67
		// Since we don't know the original mileage from here, we'll construct a known unit from known values
68
		return new Mileage($distance->to('km') / $volume->to('l'), 'kmpl');
69
	}
70
71
	/**
72
	 *
73
	 */
74
	public static function dateCmp( FuelUp $a, FuelUp $b ) {
75
		return $b->date->getTimestamp() - $a->date->getTimestamp();
76
	}
77
78
}
79