Repository: phpactor/phpactor (php)

Issue:
Autoimporting above declare strict types
Hello!

When I'm using `declare(strict_types=1);` in a file, phpactor will import classes above it which is a fatal PHP error:

![image](https://github.com/user-attachments/assets/b6e9e5d6-644b-4aca-af2a-f597be6323d1)


It should be:

![image](https://github.com/user-attachments/assets/de1b782a-5f1e-4ef2-97b2-4da91c2949a0)

Thanks!

Pull request:
Gh 2698 fix importing
fixes #2698 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f06b547b..8764277b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,10 +5,14 @@ Changelog
 
 Bug fixes:
 
+  - Fix import position when `declare` is present #2698 @dantleech
   - Fix NULL error in Docblock parser #2693 @dantleech
 
 Improvements:
 
+  - Offer completions on attributes not associated with class member body
+    #2695 @przepompownia
+  - Show prose associated with `@throws` tag #2694 @mamazu
   - Support parsing generic variance e.g. `covariant` #2664 @dantleech
 
 ## 2024-06-30
diff --git a/lib/CodeBuilder/Adapter/TolerantParser/Updater/UseStatementUpdater.php b/lib/CodeBuilder/Adapter/TolerantParser/Updater/UseStatementUpdater.php
index 9cdb7fdb..efc0e8e5 100644
--- a/lib/CodeBuilder/Adapter/TolerantParser/Updater/UseStatementUpdater.php
+++ b/lib/CodeBuilder/Adapter/TolerantParser/Updater/UseStatementUpdater.php
@@ -6,6 +6,7 @@ use Microsoft\PhpParser\Node;
 use Microsoft\PhpParser\Node\NamespaceUseClause;
 use Microsoft\PhpParser\Node\QualifiedName;
 use Microsoft\PhpParser\Node\SourceFileNode;
+use Microsoft\PhpParser\Node\Statement\DeclareStatement;
 use Microsoft\PhpParser\Node\Statement\InlineHtml;
 use Microsoft\PhpParser\Node\Statement\NamespaceDefinition;
 use Microsoft\PhpParser\Node\Statement\NamespaceUseDeclaration;
@@ -28,6 +29,9 @@ class UseStatementUpdater
             if ($childNode instanceof InlineHtml) {
                 $startNode = $node->getFirstChildNode(InlineHtml::class);
             }
+            if ($childNode instanceof DeclareStatement) {
+                $startNode = $childNode;
+            }
             if ($childNode instanceof NamespaceDefinition) {
                 $startNode = $childNode;
             }


Test patch:
diff --git a/lib/CodeTransform/Tests/Adapter/TolerantParser/Refactor/TolerantImportNameTest.php b/lib/CodeTransform/Tests/Adapter/TolerantParser/Refactor/TolerantImportNameTest.php
index dcdd6035..95e83459 100644
--- a/lib/CodeTransform/Tests/Adapter/TolerantParser/Refactor/TolerantImportNameTest.php
+++ b/lib/CodeTransform/Tests/Adapter/TolerantParser/Refactor/TolerantImportNameTest.php
@@ -66,6 +66,11 @@ class TolerantImportNameTest extends AbstractTolerantImportNameCase
             'importClass10.test',
             'Barfoo\Foobar',
         ];
+
+        yield 'with declare only' => [
+            'importClass_with_strict_types.test',
+            'Barfoo\Foobar',
+        ];
     }
 
     public function provideImportFunction(): Generator
diff --git a/lib/CodeTransform/Tests/Adapter/TolerantParser/Refactor/fixtures/importClass_with_strict_types.test b/lib/CodeTransform/Tests/Adapter/TolerantParser/Refactor/fixtures/importClass_with_strict_types.test
new file mode 100644
index 00000000..2bdaa3d6
--- /dev/null
+++ b/lib/CodeTransform/Tests/Adapter/TolerantParser/Refactor/fixtures/importClass_with_strict_types.test
@@ -0,0 +1,13 @@
+// File: source
+<?php
+
+declare(strict_types=1);
+
+new Foo<>bar();
+// File: expected
+<?php
+
+declare(strict_types=1);
+use Barfoo\Foobar;
+
+new Foobar();