Completed
Push — master ( 035a27...dfc516 )
by Rafael
03:44 queued 59s
created

Object2ArrayTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 39.71 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 1
cbo 8
dl 27
loc 68
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testCreateArray() 0 36 2
A testCreateArrayWithUnderScoreNamingStrategy() 12 12 1
A testCreateArrayWithNulls() 15 15 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
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object\Tests;
11
12
use Rafrsr\LibArray2Object\Naming\UnderscoreNamingStrategy;
13
use Rafrsr\LibArray2Object\Object2ArrayBuilder;
14
use Rafrsr\LibArray2Object\Parser\CallableParser;
15
use Rafrsr\LibArray2Object\Tests\Fixtures\Manager;
16
use Rafrsr\LibArray2Object\Tests\Fixtures\Player;
17
use Rafrsr\LibArray2Object\Tests\Fixtures\Team;
18
19
class Object2ArrayTest extends \PHPUnit_Framework_TestCase
20
{
21
    public function testCreateArray()
22
    {
23
        $team = new Team('Dream Team');
24
        $team->setCreatedAt(new \DateTime('2016-01-01'));
25
26
        $manager = new Manager();
27
        $manager->setName('Big Manager');
28
        $manager->setSalary('10000');
29
        $team->setManager($manager);
30
31
        $player1 = new Player();
32
        $player1->setName('Player 1');
33
        $player1->setNumber('1');
34
        $player1->setHeight('1.80');
35
        $team->setPlayers([$player1]);
36
37
        //register custom parser
38
        $object2Array = Object2ArrayBuilder::create()->addParser(
39
            new CallableParser(
40
                function ($value, $type, \ReflectionProperty $property, $object) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
                    if ($property->getName() === 'salary') {
42
                        $value = '$' . $value;
43
                    }
44
45
                    return $value;
46
                }
47
            )
48
        )->build();
49
50
        $array = $object2Array->createArray($team);
51
        static::assertEquals($team->getName(), $array['name']);
52
        static::assertEquals('2016-01-01 00:00:00', $array['createdAt']);
53
        static::assertEquals($manager->getName(), $array['manager']['name']);
54
        static::assertEquals('$10000', $array['manager']['salary']);
55
        static::assertEquals('Player 1', $array['players'][0]['name']);
56
    }
57
58 View Code Duplication
    public function testCreateArrayWithUnderScoreNamingStrategy()
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...
59
    {
60
        $team = new Team('Dream Team');
61
        $team->setCreatedAt(new \DateTime('2016-01-01'));
62
63
        //register custom parser
64
        $object2Array = Object2ArrayBuilder::create()->setNamingStrategy(new UnderscoreNamingStrategy())->build();
65
66
        $array = $object2Array->createArray($team);
67
        static::assertEquals($team->getName(), $array['name']);
68
        static::assertEquals('2016-01-01 00:00:00', $array['created_at']);
69
    }
70
71 View Code Duplication
    public function testCreateArrayWithNulls()
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...
72
    {
73
        $team = new Team('Dream Team');
74
75
        //register custom parser
76
        $object2Array = Object2ArrayBuilder::create()
77
            ->setIgnoreNulls(false)
78
            ->build();
79
80
        $array = $object2Array->createArray($team);
81
82
        static::assertEquals($team->getName(), $array['name']);
83
        static::assertNull($array['id']);
84
        static::assertNull($array['manager']);
85
    }
86
}
87