AbstractDatasource   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 23.4 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 8
Bugs 2 Features 4
Metric Value
wmc 16
c 8
b 2
f 4
lcom 2
cbo 2
dl 22
loc 94
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A slugify() 0 7 1
A addToIndex() 0 3 1
A findAllDepartements() 0 7 2
A findAllRegions() 0 7 2
A findAllCommunes() 0 3 1
A findCommunesByZipcode() 0 3 1
A findDepartementByCode() 0 3 1
A findDepartementByName() 11 11 2
A findRegionByName() 11 11 2
A findRegionByCode() 0 3 1
A sortByValue() 0 8 1

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
namespace Departements\Datasource;
4
5
use PhpCollection\Map;
6
7
abstract class AbstractDatasource implements DatasourceInterface
8
{
9
    protected $regions;
10
    protected $departements;
11
    protected $index;
12
13
    public function __construct() {
14
        $this->regions      = new Map();
15
        $this->departements = new Map();
16
        $this->communes     = new Map();
0 ignored issues
show
Bug introduced by
The property communes 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...
17
        $this->index = array(
18
            'departements' => array(),
19
            'regions'      => array(),
20
            'communes'     => array()
21
        );
22
    }
23
24
    protected function slugify($value) {
25
        $orginalLocale = setlocale(LC_ALL, 0);
26
        setlocale(LC_ALL, "en_US.utf8");
27
        $value = strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $value));
28
        setlocale(LC_ALL, $orginalLocale);
29
        return preg_replace("/[^a-z0-9]/", "", $value);
30
    }
31
32
    protected function addToIndex($type, $value, $key) {
33
        $this->index[$type][$this->slugify($value)] = $key;
34
    }
35
36
    public function findAllDepartements($sortByValue = false) {
37
        if (!$sortByValue) {
38
            return $this->departements;
39
        }
40
41
        return $this->sortByValue($this->departements);
42
    }
43
44
    public function findAllRegions($sortByValue = false) {
45
        if (!$sortByValue) {
46
            return $this->regions;
47
        }
48
49
        return $this->sortByValue($this->regions);
50
    }
51
52
    public function findAllCommunes() {
53
        return $this->communes;
54
    }
55
56
    public function findCommunesByZipcode($zipcode) {
57
       return $this->communes->get($zipcode)->get();
58
    }
59
60
    public function findDepartementByCode($departementCode) {
61
        return $this->departements->get($departementCode)->getOrElse(null);
62
    }
63
64 View Code Duplication
    public function findDepartementByName($departementName) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
        $departementName = $this->slugify($departementName);
66
67
        if(!isset($this->index['departements'][$departementName])) {
68
            return null;
69
        }
70
71
        $departementCode = $this->index['departements'][$departementName];
72
73
        return $this->departements->get($departementCode)->get();
74
    }
75
76 View Code Duplication
    public function findRegionByName($regionName) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
77
        $regionName = $this->slugify($regionName);
78
79
        if(!isset($this->index['regions'][$regionName])) {
80
            return null;
81
        }
82
83
        $regionCode = $this->index['regions'][$regionName];
84
85
        return $this->regions->get($regionCode)->get();
86
    }
87
88
    public function findRegionByCode($regionCode) {
89
        return $this->regions->get($regionCode)->getOrElse(null);
90
    }
91
92
    protected function sortByValue($collection) {
93
        $collator = new \Collator('fr_FR');
94
95
        $items = iterator_to_array($collection);
96
        $collator->asort($items);
97
98
        return new Map($items);
99
    }
100
}
101
102