Completed
Pull Request — 2.0 (#75)
by Julien
02:32
created

ArrayTypeConverter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 6
loc 51
c 4
b 1
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkArray() 0 13 2
A getSubtypeConverter() 6 11 2

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 Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
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 PommProject\Foundation\Converter;
11
12
use PommProject\Foundation\Exception\ConverterException;
13
use PommProject\Foundation\Session\Session;
14
15
/**
16
 * ArrayTypeConverter
17
 *
18
 * Array sub class for converters using a PHP array representation.
19
 *
20
 * @package   Foundation
21
 * @copyright 2014 - 2015 Grégoire HUBERT
22
 * @author    Grégoire HUBERT
23
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
24
 * @see       ConverterInterface
25
 * @abstract
26
 */
27
abstract class ArrayTypeConverter implements ConverterInterface
28
{
29
    protected $converters = [];
30
31
    /**
32
     * checkArray
33
     *
34
     * Check if the data is an array.
35
     *
36
     * @access protected
37
     * @param  mixed    $data
38
     * @throws ConverterException
39
     * @return array    $data
40
     */
41
    protected function checkArray($data)
42
    {
43
        if (!is_array($data)) {
44
            throw new ConverterException(
45
                sprintf(
46
                    "Array converter data must be an array ('%s' given).",
47
                    gettype($data)
48
                )
49
            );
50
        }
51
52
        return $data;
53
    }
54
55
    /**
56
     * getSubtypeConverter
57
     *
58
     * Since the arrays in PostgreSQL have the same subtype, it is useful to
59
     * cache it here to avoid summoning the ClientHolder all the time.
60
     *
61
     * @access protected
62
     * @param  string   $type
63
     * @param  Session  $session
64
     * @return ConverterInterface
65
     */
66
    protected function getSubtypeConverter($type, Session $session)
67
    {
68 View Code Duplication
        if (!isset($this->converters[$type])) {
69
            $this->converters[$type] = $session
70
                ->getClientUsingPooler('converter', $type)
71
                ->getConverter()
72
                ;
73
        }
74
75
        return $this->converters[$type];
76
    }
77
}
78