Completed
Push — master ( e8fccd...9c2126 )
by Harry
02:44
created

ParserTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 47.83 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 22
loc 46
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructorValueParsers() 0 8 1
A testAddingAParser() 11 11 1
A testRemovingAParser() 11 11 1
A testParserIteratesSoNothingShouldHappenIfThereIsNoRequestForData() 0 10 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 Graze\CsvToken\Test\Unit;
4
5
use Graze\CsvToken\Parser;
6
use Graze\CsvToken\Test\TestCase;
7
use Graze\CsvToken\ValueParser\ValueParserInterface;
8
use Iterator;
9
use Mockery as m;
10
use Mockery\MockInterface;
11
12
class ParserTest extends TestCase
13
{
14
    public function testConstructorValueParsers()
15
    {
16
        /** @var ValueParserInterface|MockInterface $valueParser */
17
        $valueParser = m::mock(ValueParserInterface::class);
18
19
        $parser = new Parser([$valueParser]);
0 ignored issues
show
Documentation introduced by
array($valueParser) is of type array<integer,object<Gra...kery\\MockInterface>"}>, but the function expects a array<integer,object<Gra...\ValueParserInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
20
        static::assertTrue($parser->hasValueParser($valueParser));
0 ignored issues
show
Bug introduced by
It seems like $valueParser defined by \Mockery::mock(\Graze\Cs...ParserInterface::class) on line 17 can also be of type object<Mockery\MockInterface>; however, Graze\CsvToken\Parser::hasValueParser() does only seem to accept object<Graze\CsvToken\Va...r\ValueParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
21
    }
22
23 View Code Duplication
    public function testAddingAParser()
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...
24
    {
25
        /** @var ValueParserInterface|MockInterface $valueParser */
26
        $valueParser = m::mock(ValueParserInterface::class);
27
28
        $parser = new Parser();
29
        static::assertFalse($parser->hasValueParser($valueParser));
0 ignored issues
show
Bug introduced by
It seems like $valueParser defined by \Mockery::mock(\Graze\Cs...ParserInterface::class) on line 26 can also be of type object<Mockery\MockInterface>; however, Graze\CsvToken\Parser::hasValueParser() does only seem to accept object<Graze\CsvToken\Va...r\ValueParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
30
31
        $parser->addValueParser($valueParser);
0 ignored issues
show
Bug introduced by
It seems like $valueParser defined by \Mockery::mock(\Graze\Cs...ParserInterface::class) on line 26 can also be of type object<Mockery\MockInterface>; however, Graze\CsvToken\Parser::addValueParser() does only seem to accept object<Graze\CsvToken\Va...r\ValueParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
        static::assertTrue($parser->hasValueParser($valueParser));
0 ignored issues
show
Bug introduced by
It seems like $valueParser defined by \Mockery::mock(\Graze\Cs...ParserInterface::class) on line 26 can also be of type object<Mockery\MockInterface>; however, Graze\CsvToken\Parser::hasValueParser() does only seem to accept object<Graze\CsvToken\Va...r\ValueParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
33
    }
34
35 View Code Duplication
    public function testRemovingAParser()
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...
36
    {
37
        /** @var ValueParserInterface|MockInterface $valueParser */
38
        $valueParser = m::mock(ValueParserInterface::class);
39
40
        $parser = new Parser([$valueParser]);
0 ignored issues
show
Documentation introduced by
array($valueParser) is of type array<integer,object<Gra...kery\\MockInterface>"}>, but the function expects a array<integer,object<Gra...\ValueParserInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
42
        static::assertTrue($parser->hasValueParser($valueParser));
0 ignored issues
show
Bug introduced by
It seems like $valueParser defined by \Mockery::mock(\Graze\Cs...ParserInterface::class) on line 38 can also be of type object<Mockery\MockInterface>; however, Graze\CsvToken\Parser::hasValueParser() does only seem to accept object<Graze\CsvToken\Va...r\ValueParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
        $parser->removeValueParser($valueParser);
0 ignored issues
show
Bug introduced by
It seems like $valueParser defined by \Mockery::mock(\Graze\Cs...ParserInterface::class) on line 38 can also be of type object<Mockery\MockInterface>; however, Graze\CsvToken\Parser::removeValueParser() does only seem to accept object<Graze\CsvToken\Va...r\ValueParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44
        static::assertFalse($parser->hasValueParser($valueParser));
0 ignored issues
show
Bug introduced by
It seems like $valueParser defined by \Mockery::mock(\Graze\Cs...ParserInterface::class) on line 38 can also be of type object<Mockery\MockInterface>; however, Graze\CsvToken\Parser::hasValueParser() does only seem to accept object<Graze\CsvToken\Va...r\ValueParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45
    }
46
47
    public function testParserIteratesSoNothingShouldHappenIfThereIsNoRequestForData()
48
    {
49
        $parser = new Parser();
50
51
        $tokens = m::mock(Iterator::class);
52
53
        $output = $parser->parse($tokens);
54
55
        static::assertInstanceOf(Iterator::class, $output);
56
    }
57
}
58