Completed
Push — master ( cb4e34...0670fd )
by Rudie
03:57
created

FuelUp::createFromTrend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 8
rs 9.4285
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
17
	/**
18
	 *
19
	 */
20
	public static function createFromTrend( Vehicle $vehicle, array $fuelup, InputConversion $input = null ) {
21
		// Trend is always in real numbers, and only its natives are reliable so use those
22
		$input or $input = $client->createTrendInputConversion();
0 ignored issues
show
Bug introduced by
The variable $client does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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...
23
24
		// @todo Parse date correctly
25
		$date = DateTime::createFromFormat('Y-m-d H:i:s', $fuelup['fuelup_date']);
26
		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 25 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...
27
	}
28
29
	/**
30
	 *
31
	 */
32
	public static function createFromDetail( Vehicle $vehicle, array $fuelup, InputConversion $input = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
		// @todo Parse date correctly
34
		$date = DateTime::createFromFormat('d-m-y', $fuelup['fuelup_date']);
35
		return new static($vehicle, $date, $fuelup['miles_last_fuelup'], $fuelup['amount']);
0 ignored issues
show
Security Bug introduced by
It seems like $date defined by \DateTime::createFromFor...$fuelup['fuelup_date']) on line 34 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...
36
	}
37
38
	/**
39
	 *
40
	 */
41
	protected function __construct( Vehicle $vehicle, DateTime $date, $raw_distance, $raw_amount, InputConversion $input = null ) {
42
		$this->vehicle = $vehicle;
43
44
		$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...
45
46
		$this->date = $date;
47
48
		$this->raw_amount = $input->convertNumber($raw_amount);
1 ignored issue
show
Bug introduced by
The property raw_amount does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like $input is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
49
		$this->raw_distance = $input->convertNumber($raw_distance);
0 ignored issues
show
Bug introduced by
The property raw_distance does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
51
		$this->amount = $input->convertVolume($this->raw_amount);
0 ignored issues
show
Bug introduced by
The property amount does not seem to exist. Did you mean raw_amount?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
		$this->distance = $input->convertDistance($this->raw_distance);
0 ignored issues
show
Bug introduced by
The property distance does not seem to exist. Did you mean raw_distance?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
54
		$this->mileage = static::createMileage($this->distance, $this->amount);
0 ignored issues
show
Bug introduced by
The property mileage does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property distance does not seem to exist. Did you mean raw_distance?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property amount does not seem to exist. Did you mean raw_amount?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
55
56
		// $this->original = $fuelup;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
	}
58
59
	/**
60
	 *
61
	 */
62
	public static function createMileage( Length $distance, Volume $amount ) {
63
		// Since we don't know the original mileage from here, we'll construct a known unit from known values
64
		return new Mileage($distance->to('km') / $amount->to('l'), 'kmpl');
65
	}
66
67
	/**
68
	 *
69
	 */
70
	public static function dateCmp( FuelUp $a, FuelUp $b ) {
71
		return $b->date->getTimestamp() - $a->date->getTimestamp();
72
	}
73
74
}
75