Skip to content

Commit

Permalink
Replace substr in getCode() to cover more cases (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackskyliner authored and GrahamCampbell committed Sep 9, 2017
1 parent 53f7fe1 commit 69e7735
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/ClassPreloader.php
Expand Up @@ -128,13 +128,11 @@ public function getCode($file, $comments = true)
$stmts = $this->traverser->traverseFile($parsed, $file);
$pretty = $this->printer->prettyPrint($stmts);

if (substr($pretty, 30) === '<?php declare(strict_types=1);' || substr($pretty, 30) === "<?php\ndeclare(strict_types=1);") {
$pretty = substr($pretty, 32);
} elseif (substr($pretty, 31) === "<?php\r\ndeclare(strict_types=1);") {
$pretty = substr($pretty, 33);
} elseif (substr($pretty, 5) === '<?php') {
$pretty = substr($pretty, 7);
}
$pretty = preg_replace(
'#^(<\?php)?[\s]*(/\*\*?.*?\*/)?[\s]*(declare[\s]*\([\s]*strict_types[\s]*=[\s]*1[\s]*\);)?#s',
'',
$pretty
);

return $this->getCodeWrappedIntoNamespace($parsed, $pretty);
}
Expand Down
55 changes: 55 additions & 0 deletions tests/ClassPreloaderTest.php
@@ -0,0 +1,55 @@
<?php

use ClassPreloader\ClassPreloader;
use ClassPreloader\Parser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;

class ClassPreloaderTest extends PHPUnit_Framework_TestCase
{
/**
* This tests the correct detection and stripping of strict_types declarations while preloading files.
*/
public function testStripStrictTypeDeclaration()
{
$printer = $this->getMockBuilder(PrettyPrinter::class)
->disableOriginalConstructor()
->setMethods(['prettyPrint']);
$parser = $this->getMockBuilder(Parser::class)
->disableOriginalConstructor()
->setMethods(['parse', 'getErrors']);
$traverser = $this->getMockBuilder(NodeTraverser::class)
->disableOriginalConstructor()
->setMethods(['traverseFile']);

$parserMock = $parser->getMock();
$parserMock->expects($this->once())
->method('parse')
->willReturn([]);
$traverserMock = $traverser->getMock();
$traverserMock->expects($this->once())
->method('traverseFile')
->willReturn([]);
$printerMock = $printer->getMock();
$printerMock->expects($this->once())
->method('prettyPrint')
->willReturn(
file_get_contents(__DIR__.'/stubs/StrictClassWithComments.php')
);

$classPreloader = new ClassPreloader(
$printerMock,
$parserMock,
$traverserMock
);

$code = $classPreloader->getCode(__DIR__.'/stubs/StrictClassWithComments.php');

// $code should not have 'declare(strict_types=1)' declarations.
$this->assertNotRegExp(
'/(.*?)declare\s*\(strict_types\s*=\s*1\)(.*?)/mi',
$code,
'Generated ClassPreloader output should correctly detect and strip strict_type declare statements.'
);
}
}
12 changes: 12 additions & 0 deletions tests/stubs/StrictClassWithComments.php
@@ -0,0 +1,12 @@
<?php
/**
* This is some copyright or file comment, popular in some libraries.
*/
declare(strict_types=1);

/**
* Not some Class comment.
*/
class StrictClassWithComments
{
}

0 comments on commit 69e7735

Please sign in to comment.