Completed
Push — master ( eabfbb...93a568 )
by Milan
01:38
created

ADriver::download()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 7.1428
c 0
b 0
f 0
cc 8
eloc 14
nc 8
nop 2
1
<?php
2
3
namespace h4kuna\Exchange\Driver;
4
5
use DateTime,
6
	h4kuna\Exchange;
7
8
/**
9
 * Download currency from server.
10
 */
11
abstract class ADriver
12
{
13
14
	/** @var \DateTime */
15
	private $date;
16
17
	/**
18
	 * Download data from remote source and save.
19
	 * @param DateTime $date
20
	 * @param array $allowedCurrencies
21
	 * @return Exchange\Currency\ListRates
22
	 */
23
	public function download(DateTime $date = NULL, array $allowedCurrencies = [])
24
	{
25
		if ($allowedCurrencies) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $allowedCurrencies of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
26
			$allowedCurrencies = array_flip($allowedCurrencies);
27
		}
28
		$source = $this->loadFromSource($date);
29
		$currencies = new Exchange\Currency\ListRates($this->getDate());
30
		foreach ($source as $row) {
31
			if (!$row) {
32
				continue;
33
			}
34
			$property = $this->createProperty($row);
35
36
			if (!$property || !$property->rate || ($allowedCurrencies !== [] && !isset($allowedCurrencies[$property->code]))) {
37
				continue;
38
			}
39
			$currencies->addProperty($property);
40
		}
41
		$currencies->getFirst(); // check if is not empty
42
		return $currencies;
43
	}
44
45
	protected function setDate($format, $value)
46
	{
47
		$this->date = DateTime::createFromFormat($format, $value);
0 ignored issues
show
Documentation Bug introduced by
It seems like \DateTime::createFromFormat($format, $value) can also be of type false. However, the property $date is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
48
		$this->date->setTime(0, 0, 0);
49
	}
50
51
	public function getName()
52
	{
53
		return strtolower(str_replace('\\', '.', static::class));
54
	}
55
56
	/**
57
	 * @return DateTime
58
	 */
59
	public function getDate()
60
	{
61
		return $this->date;
62
	}
63
64
	/**
65
	 * Load data for iterator.
66
	 * @param DateTime|NULL $date
67
	 * @return array
68
	 */
69
	abstract protected function loadFromSource(DateTime $date = NULL);
70
71
	/**
72
	 * Modify data before save to cache.
73
	 * @return Exchange\Currency\Property|NULL
74
	 */
75
	abstract protected function createProperty($row);
76
77
}
78