diff --git a/src/TokenRemover.php b/src/TokenRemover.php
index <HASH>..<HASH> 100644
--- a/src/TokenRemover.php
+++ b/src/TokenRemover.php
@@ -17,7 +17,10 @@ final class TokenRemover
     {
         self::removeTrailingHorizontalWhitespaces($tokens, $tokens->getNonEmptySibling($index, -1));
 
-        self::removeLeadingNewline($tokens, $tokens->getNonEmptySibling($index, 1));
+        $nextIndex = $tokens->getNonEmptySibling($index, 1);
+        if ($nextIndex !== null) {
+            self::removeLeadingNewline($tokens, $tokens->getNonEmptySibling($index, 1));
+        }
 
         $tokens->clearTokenAndMergeSurroundingWhitespace($index);
     }
diff --git a/tests/TokenRemoverTest.php b/tests/TokenRemoverTest.php
index <HASH>..<HASH> 100644
--- a/tests/TokenRemoverTest.php
+++ b/tests/TokenRemoverTest.php
@@ -145,5 +145,12 @@ namespace Foo;
 /** Some comment */    namespace Foo;
 ',
         ];
+
+        yield [
+            '<?php
+',
+            '<?php
+// comment as last element',
+        ];
     }
 }
TokenRemover - fix for comment as last element