DuplicateIterator::current()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Import\Iterator;
3
4
use FMUP\Import\Config;
5
use FMUP\Import\Exception;
6
7
/**
8
 * Permet de repérer les éventuels doublons présents dans le fichier
9
 *
10
 * @author csanz
11
 *
12
 */
13
class DuplicateIterator extends \IteratorIterator
14
{
15
    /**
16
     *
17
     * @var int[]
18
     */
19
    private $keyList = array();
20
    private $currentKey = null;
21
22
    /**
23
     * (non-PHPdoc)
24
     *
25
     * @see IteratorIterator::next()
26
     * @return mixed
27
     * @throws Exception
28
     */
29 2
    public function current()
30
    {
31 2
        $current = $this->getInnerIterator()->current();
32 2
        $currentKey = $this->getInnerIterator()->key();
33 2
        if ($this->currentKey !== $currentKey) {
34 2
            $this->currentKey = $currentKey;
35 2
            if (!$current instanceof Config) {
36 1
                throw new Exception('Current object is not config');
37
            }
38 1
            $this->checkDuplicate($current);
39
        }
40 1
        return $current;
41
    }
42
43
    /**
44
     * Vérifie l'unicité des objets de la ligne
45
     *
46
     * @param Config $current
47
     * @return bool true if duplicate found
48
     */
49 1
    public function checkDuplicate(Config $current)
50
    {
51 1
        $str = '';
52
        // on concatène tous les champs obligatoires
53 1
        foreach ($current->getListeConfigObjet() as $configObject) {
54 1
            foreach ($configObject->getListeIndexChamp() as $index) {
55 1
                $str .= $current->getField($index)->getValue() . ';';
56
            }
57
        }
58 1
        $uniqueKey = sha1($str);
59 1
        $key = isset($this->keyList[$uniqueKey]) ? $this->keyList[$uniqueKey] : false;
60 1
        $current->setDoublonLigne($key);
0 ignored issues
show
Security Bug introduced by
It seems like $key defined by isset($this->keyList[$un...ist[$uniqueKey] : false on line 59 can also be of type false; however, FMUP\Import\Config::setDoublonLigne() does only seem to accept integer, 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...
61 1
        if ($key === false) {
62
            // si on ne le trouve pas, on l'ajoute dans le tableau
63 1
            $this->keyList[$uniqueKey] = $this->getInnerIterator()->key();
64
        }
65 1
        return (bool)$key;
66
    }
67
}
68