RateFilterUtil   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 16.39 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 10
loc 61
rs 10
c 0
b 0
f 0
ccs 20
cts 20
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A matches() 0 16 6
A matchesDateCriteria() 10 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\ExchangeRate\Utils;
11
12
use RunOpenCode\ExchangeRate\Contract\RateInterface;
13
14
/**
15
 * Class RateFilterUtil
16
 *
17
 * Utility to check if rate matches filters.
18
 *
19
 * @package RunOpenCode\ExchangeRate\Utils
20
 */
21
final class RateFilterUtil
22
{
23
    use FilterUtilHelper;
24
25
    private function __construct() { /* noop */ }
26
27
    /**
28
     * Check if rate matches filters.
29
     *
30
     * @param RateInterface $rate Rate to filter.
31
     * @param array $criteria Filter criteria.
32
     * @return bool TRUE if filter criteria is matched.
33
     */
34 8
    public static function matches(RateInterface $rate, array $criteria)
35
    {
36
        return
37 8
            self::matchesArrayCriteria('currencyCode', $rate, $criteria)
38
            &&
39 8
            self::matchesArrayCriteria('sourceName', $rate, $criteria)
40
            &&
41 8
            self::matchesArrayCriteria('rateType', $rate, $criteria)
42
            &&
43 8
            self::matchesDateCriteria('onDate', $rate, $criteria)
44
            &&
45 8
            self::matchesDateCriteria('dateFrom', $rate, $criteria)
46
            &&
47 8
            self::matchesDateCriteria('dateTo', $rate, $criteria)
48
            ;
49
    }
50
51
    /**
52
     * Check if date criteria is matched.
53
     *
54
     * @param string $key Date criteria key.
55
     * @param RateInterface $rate Rate to check for match.
56
     * @param array $criteria Filter criterias.
57
     * @return bool TRUE if there is a match.
58
     */
59 8
    private static function matchesDateCriteria($key, RateInterface $rate, array $criteria)
60
    {
61 8
        $date = self::extractDateCriteria($key, $criteria);
62
63 8
        if ($date === null) {
64 8
            return true;
65
        }
66
67 3 View Code Duplication
        if ($key === 'dateFrom') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68 3
            $rateDate = new \DateTime($rate->getDate()->format(\DateTime::ATOM));
69 3
            $rateDate->setTime(23, 59, 59);
70 3
            return $date <= $rateDate;
71
        }
72
73 3 View Code Duplication
        if ($key === 'dateTo') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74 3
            $rateDate = new \DateTime($rate->getDate()->format(\DateTime::ATOM));
75 3
            $rateDate->setTime(0, 0, 0);
76 3
            return $date >= $rateDate;
77
        }
78
79 3
        return $date->format('Y-m-d') === $rate->getDate()->format('Y-m-d');
80
    }
81
}
82