Completed
Push — master ( 3e2e6d...57a730 )
by Rémi
02:01
created

Map::map()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * inetprocess/transformation
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @author Rémi Sauvat
8
 * @copyright 2005-2015 iNet Process
9
 *
10
 * @package inetprocess/transformation
11
 *
12
 * @license GNU General Public License v2.0
13
 *
14
 * @link http://www.inetprocess.com
15
 */
16
17
namespace Inet\Transformation\Rule;
18
19
use Inet\Transformation\Exception\TransformationException;
20
21
/**
22
 * Call a function and send back the result
23
 */
24
class Map extends AbstractRule
25
{
26
    protected $mapping = array();
27
    /**
28
     * Operate the transformation
29
     *
30
     * @param string $input
31
     * @param array  $arguments
32
     *
33
     * @throws Inet\Transformation\Exception\TransformationException
34
     *
35
     * @return string
36
     */
37 8
    public function transform($input, $arguments)
38
    {
39
        // I should have two arguments: old format / new format
40 8
        if (count($arguments) !== 1) {
41 2
            throw new TransformationException(
42
                'Rule Map expects exactly 1 argument'
43 2
            );
44
        }
45 6
        $mapping = $arguments[0];
46 6
        if (!is_array($mapping)) {
47 1
            throw new TransformationException(
48
                'First argument of Map should by an assosiative array'
49 1
            );
50
        }
51 5
        $this->mapping = $mapping;
52
53 5
        if (!is_array($input)) {
54 4
            return $this->map($input);
55
        }
56
        // More complex version if input is an array
57
        // Map each cell independently
58
        // And return an array
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59 1
        return array_map(array($this, 'map'), $input);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_map(array($this, 'map'), $input); (array) is incompatible with the return type documented by Inet\Transformation\Rule\Map::transform of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
62 5
    public function map($value)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
63
    {
64
        // If not array map and return fast
65 5
        if (array_key_exists($value, $this->mapping)) {
66 2
            return $this->mapping[$value];
67
        }
68
        // Couldn't map so return as is
69 4
        return $value;
70
    }
71
}
72