From 61f2c4288bc6027fcdb347ace448d0936bbdcda4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 22 Apr 2023 15:28:53 +0700 Subject: [PATCH] [TypeDeclaration] Handle mix InlineHTML on FileWithoutNamespace (#3648) * Added failling declare-strict test currently declare-strict is added twice * rename fixture * fix * Fix phsptan * add more fixture: only inline html --------- Co-authored-by: Markus Staab --- .../Fixture/mix_next_inline_html.php.inc | 18 ++++++++++++++++++ .../Fixture/only_inline_html.php.inc | 12 ++++++++++++ .../DeclareStrictTypesRector.php | 10 ++++++++++ 3 files changed, 40 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/mix_next_inline_html.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/only_inline_html.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/mix_next_inline_html.php.inc b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/mix_next_inline_html.php.inc new file mode 100644 index 00000000000..2421c95559b --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/mix_next_inline_html.php.inc @@ -0,0 +1,18 @@ + +
+ Hello +
+----- + +
+ Hello +
diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/only_inline_html.php.inc b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/only_inline_html.php.inc new file mode 100644 index 00000000000..2addca98e6e --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/only_inline_html.php.inc @@ -0,0 +1,12 @@ +
+ Hello +
+----- + +
+ Hello +
diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php index 864d87de752..df165047312 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -7,11 +7,13 @@ use PhpParser\Node; use PhpParser\Node\Identifier; use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Declare_; use PhpParser\Node\Stmt\DeclareDeclare; use PhpParser\Node\Stmt\Nop; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; +use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\Core\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -59,6 +61,14 @@ public function beforeTraverse(array $nodes): ?array $stmt = current($newStmts); + if ($stmt instanceof FileWithoutNamespace) { + $stmt = current($stmt->stmts); + + if (! $stmt instanceof Stmt) { + return null; + } + } + // when first stmt is Declare_, verify if there is strict_types definition already, // as multiple declare is allowed, with declare(strict_types=1) only allowed on very first stmt if ($stmt instanceof Declare_) {