Skip to content

Commit 0c06899

Browse files
committed
Only add strict_types back in if it wasn't added by transformer
1 parent 13a0325 commit 0c06899

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/CodePatcher.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Error;
88
use PhpParser\Node\Stmt;
9+
use PhpParser\Node\Stmt\Declare_;
910
use PhpParser\Parser;
1011
use PhpParser\PrettyPrinter\Standard;
1112
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
@@ -91,7 +92,7 @@ private function applyPatch(Patch $patch, string $code): string
9192
}
9293

9394
$declare = null;
94-
if (isset($statements[0]) && $statements[0] instanceof \PhpParser\Node\Stmt\Declare_) {
95+
if ($this->isFirstStatementStrictTypesDeclare($statements)) {
9596
$declare = array_shift($statements);
9697
}
9798

@@ -107,13 +108,18 @@ private function applyPatch(Patch $patch, string $code): string
107108
}
108109
}
109110

110-
if ($declare !== null) {
111+
if ($declare !== null && !$this->isFirstStatementStrictTypesDeclare($statements)) {
111112
array_unshift($statements, $declare);
112113
}
113114

114115
return $this->printer->prettyPrintFile($statements);
115116
}
116117

118+
public function isFirstStatementStrictTypesDeclare(array $statements): bool
119+
{
120+
return isset($statements[0]) && $statements[0] instanceof Declare_;
121+
}
122+
117123
/**
118124
* @param CodeInsertion $codeInsertion
119125
* @param array<Stmt> $statements

test/CodePatcherTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use PhpParser\Node\Expr\Variable;
66
use PhpParser\Node\Name;
7+
use PhpParser\Node\Scalar\LNumber;
78
use PhpParser\Node\Stmt\Catch_;
9+
use PhpParser\Node\Stmt\DeclareDeclare;
810
use PhpParser\Node\Stmt\TryCatch;
911
use PhpParser\ParserFactory;
1012
use PhpParser\PrettyPrinter\Standard;
13+
use PhpParser\PrettyPrinterAbstract;
1114
use PhpSchool\PhpWorkshop\CodePatcher;
1215
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1316
use PhpSchool\PhpWorkshop\Patch;
@@ -169,4 +172,32 @@ public function testTransformerWithStrictTypes(): void
169172
$patcher->patch($exercise, $code)
170173
);
171174
}
175+
176+
public function testTransformerWhichAddsStrictTypesDoesNotResultInDoubleStrictTypesStatement(): void
177+
{
178+
$code = '<?php declare(strict_types=1); $original = true;';
179+
$patch = (new Patch())
180+
->withTransformer(function (array $statements) {
181+
return [new \PhpParser\Node\Stmt\Declare_([
182+
new DeclareDeclare(
183+
new \PhpParser\Node\Identifier('strict_types'),
184+
new LNumber(1)
185+
)
186+
])];
187+
});
188+
189+
$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard());
190+
191+
$exercise = $this->createMock(PatchableExercise::class);
192+
193+
$exercise
194+
->expects($this->once())
195+
->method('getPatch')
196+
->willReturn($patch);
197+
198+
$this->assertEquals(
199+
"<?php\n\ndeclare (strict_types=1);",
200+
$patcher->patch($exercise, $code)
201+
);
202+
}
172203
}

0 commit comments

Comments
 (0)