Completed
Push — master ( 3da0b9...07614c )
by Kyle
55:56 queued 41:21
created

testVoidTypeHintReturnNamespaced()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 7
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of PDepend.
4
 *
5
 * Copyright (c) 2008-2017 Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in
17
 *     the documentation and/or other materials provided with the
18
 *     distribution.
19
 *
20
 *   * Neither the name of Manuel Pichler nor the names of his
21
 *     contributors may be used to endorse or promote products derived
22
 *     from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @copyright 2008-2017 Manuel Pichler. All rights reserved.
38
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
39
 */
40
41
namespace PDepend\Source\Language\PHP;
42
43
use PDepend\AbstractTest;
44
use PDepend\Source\Builder\Builder;
45
use PDepend\Source\Tokenizer\Tokenizer;
46
use PDepend\Util\Cache\CacheDriver;
47
48
/**
49
 * Test case for the {@link \PDepend\Source\Language\PHP\PHPParserVersion71} class.
50
 *
51
 * @copyright 2008-2017 Manuel Pichler. All rights reserved.
52
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
53
 * @covers \PDepend\Source\Language\PHP\PHPParserVersion71
54
 * @group unittest
55
 */
56
class PHPParserVersion71Test extends AbstractTest
57
{
58
    /**
59
     * @return void
60
     */
61
    public function testConstVisibility()
62
    {
63
        $this->assertNotNull($this->parseCodeResourceForTest());
64
    }
65
66
    /**
67
     * @return void
68
     */
69
    public function testConstVisibilityInInterfacePublic()
70
    {
71
        $this->assertNotNull($this->parseCodeResourceForTest());
72
    }
73
74
    /**
75
     * @return void
76
     */
77
    public function testConstVisibilityInInterfaceProtected()
78
    {
79
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
            '\\PDepend\\Source\\Parser\\InvalidStateException',
81
            'Constant can\'t be declared private or protected in interface "TestInterface".'
82
        );
83
84
        $this->parseCodeResourceForTest();
85
    }
86
87
    /**
88
     * @return void
89
     */
90
    public function testConstVisibilityInInterfacePrivate()
91
    {
92
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
93
            '\\PDepend\\Source\\Parser\\InvalidStateException',
94
            'Constant can\'t be declared private or protected in interface "TestInterface".'
95
        );
96
        
97
        $this->parseCodeResourceForTest();
98
    }
99
100
    /**
101
     * @return void
102
     */
103
    public function testCatchMultipleExceptionClasses()
104
    {
105
        $this->assertNotNull($this->parseCodeResourceForTest());        
106
    }
107
    
108
    /**
109
     * @return void
110
     */
111
    public function testNullableTypeHintParameter()
112
    {
113
        $this->assertNotNull($this->parseCodeResourceForTest());
114
    }
115
116
    /**
117
     * @return void
118
     */
119
    public function testNullableTypeHintReturn()
120
    {
121
        $this->assertNotNull($this->parseCodeResourceForTest());
122
    }
123
124
    /**
125
     * @return void
126
     */
127
    public function testIterableTypeHintParameter()
128
    {
129
        $type = $this->getFirstFormalParameterForTestCase()->getType();
130
131
        $this->assertFalse($type->isScalar());
132
        $this->assertTrue($type->isArray());
133
        $this->assertSame('iterable', $type->getImage());
134
    }
135
136
    /**
137
     * @return void
138
     */
139 View Code Duplication
    public function testIterableTypeHintReturn()
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...
140
    {
141
        $type = $this->getFirstFunctionForTestCase()->getReturnType();
142
        
143
        $this->assertFalse($type->isScalar());
144
        $this->assertTrue($type->isArray());
145
        $this->assertSame('iterable', $type->getImage());
146
    }
147
148
    /**
149
     * @return void
150
     */
151 View Code Duplication
    public function testVoidTypeHintReturn()
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...
152
    {
153
        $type = $this->getFirstFunctionForTestCase()->getReturnType();
154
        
155
        $this->assertTrue($type->isScalar());
156
        $this->assertFalse($type->isArray());
157
        $this->assertSame('void', $type->getImage());
158
    }
159
160
    /**
161
     * @return void
162
     */
163 View Code Duplication
    public function testVoidTypeHintReturnNamespaced()
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...
164
    {
165
        $type = $this->getFirstFunctionForTestCase()->getReturnType();
166
        $this->assertTrue($type->isScalar());
167
        $this->assertFalse($type->isArray());
168
        $this->assertSame('void', $type->getImage());
169
    }
170
171
    /**
172
     * @param \PDepend\Source\Tokenizer\Tokenizer $tokenizer
173
     * @param \PDepend\Source\Builder\Builder $builder
174
     * @param \PDepend\Util\Cache\CacheDriver $cache
175
     * @return \PDepend\Source\Language\PHP\AbstractPHPParser
176
     */
177
    protected function createPHPParser(Tokenizer $tokenizer, Builder $builder, CacheDriver $cache)
178
    {
179
        return $this->getMockForAbstractClass(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getMockFor...er, $builder, $cache)); (PHPUnit_Framework_MockObject_MockObject) is incompatible with the return type of the parent method PDepend\AbstractTest::createPHPParser of type PDepend\Source\Language\PHP\PHPParserGeneric.

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...
180
            'PDepend\\Source\\Language\\PHP\\PHPParserVersion71',
181
            array($tokenizer, $builder, $cache)
182
        );
183
    }
184
}
185