diff --git a/composer.json b/composer.json index 468546c4..52c182bf 100644 --- a/composer.json +++ b/composer.json @@ -40,5 +40,8 @@ "branch-alias": { "dev-master": "4.0-dev" } + }, + "require-dev": { + "phpunit/phpunit": "^7.3" } } diff --git a/src/Detector/Strategy/DefaultStrategy.php b/src/Detector/Strategy/DefaultStrategy.php index af356bc5..74a79ff9 100644 --- a/src/Detector/Strategy/DefaultStrategy.php +++ b/src/Detector/Strategy/DefaultStrategy.php @@ -35,6 +35,9 @@ public function processFile(string $file, int $minLines, int $minTokens, CodeClo foreach (\array_keys($tokens) as $key) { $token = $tokens[$key]; + if (is_string($token) && $token === '?') { + $token = [0, '?', $lastTokenLine]; + } if (\is_array($token)) { if (!isset($this->tokensIgnoreList[$token[0]])) { if ($tokenNr === 0) { diff --git a/tests/DetectorTest.php b/tests/DetectorTest.php index bbac1230..9e959d5c 100644 --- a/tests/DetectorTest.php +++ b/tests/DetectorTest.php @@ -46,7 +46,7 @@ public function testDetectingSimpleClonesWorks($strategy): void $this->assertEquals(TEST_FILES_PATH . 'Math.php', $file->getName()); $this->assertEquals(139, $file->getStartLine()); $this->assertEquals(59, $clones[0]->getSize()); - $this->assertEquals(136, $clones[0]->getTokens()); + $this->assertEquals(138, $clones[0]->getTokens()); $this->assertEquals( ' public function div($v1, $v2) @@ -288,6 +288,27 @@ public function testStripComments($strategy): void $this->assertCount(1, $clones); } + /** + * @covers SebastianBergmann\PHPCPD\Detector\Detector::copyPasteDetection + * @dataProvider strategyProvider + * + * @param string $strategy + */ + public function testNullableTypeHints(string $strategy): void + { + $detector = new SebastianBergmann\PHPCPD\Detector\Detector(new $strategy); + $clones = $detector->copyPasteDetection( + [ + TEST_FILES_PATH . 'NullableTypeHint.php', + ], + 2, + 7, + true + ); + $clones = $clones->getClones(); + $this->assertCount(0, $clones); + } + public function strategyProvider() { return [ diff --git a/tests/_files/NullableTypeHint.php b/tests/_files/NullableTypeHint.php new file mode 100644 index 00000000..7fd9224c --- /dev/null +++ b/tests/_files/NullableTypeHint.php @@ -0,0 +1,17 @@ +