Skip to content

[TypeDeclaration] Skip with include on SafeDeclareStrictTypesRector - #7862

Merged
TomasVotruba merged 4 commits into
mainfrom
skip-with-include
Jan 28, 2026
Merged

[TypeDeclaration] Skip with include on SafeDeclareStrictTypesRector#7862
TomasVotruba merged 4 commits into
mainfrom
skip-with-include

Conversation

@samsonasik

Copy link
Copy Markdown
Member

No description provided.

@TomasVotruba TomasVotruba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@samsonasik

Copy link
Copy Markdown
Member Author

Fixed 🎉 /cc @calebdw

@samsonasik

Copy link
Copy Markdown
Member Author

@TomasVotruba ready 👍

@TomasVotruba
TomasVotruba merged commit 367edb5 into main Jan 28, 2026
57 checks passed
@TomasVotruba
TomasVotruba deleted the skip-with-include branch January 28, 2026 16:10
@TomasVotruba

Copy link
Copy Markdown
Member

LGTM 👍

@calebdw

calebdw commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

@samsonasik, did you actually run into a PHP error or are you just preemptively making this change?

My testing shows that this should not have been merged---declare(strict_types=1) only affects the file that it is defined in and not code in other files

mkdir -p /tmp/strict-test && cd /tmp/strict-test

# A non-strict included file that uses type coercion internally
cat > included.php << 'PHP'
<?php
function greet(string $name): string {
    return "Hello, " . $name;
}
// Type coercion (int -> string) works because THIS file is non-strict
echo greet(123) . "\n";
PHP

# Main file WITHOUT strict_types
cat > without_strict.php << 'PHP'
<?php
include __DIR__ . '/included.php';
echo greet("world") . "\n";
PHP

# Main file WITH strict_types (what Rector would produce)
cat > with_strict.php << 'PHP'
<?php
declare(strict_types=1);
include __DIR__ . '/included.php';
echo greet("world") . "\n";
PHP

php without_strict.php
# Hello, 123
# Hello, world

php with_strict.php
# Hello, 123   <-- included file's internal call still works with NO errors!
# Hello, world

In short, it is perfectly safe to add declare(strict_types=1); to a file that includes non-strict code as long as that file itself uses strict types

@samsonasik

Copy link
Copy Markdown
Member Author

@calebdw I see, I just realize that declare(strict_types=1) is file scoped, let's revert :)

@calebdw

calebdw commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

Thanks! I've found it's important to test assumption about strict_types because it's not always immediately obvious

@TomasVotruba

Copy link
Copy Markdown
Member

@calebdw Btw, I'd love to give this rule more attention. Would you open to write a simple post how/why it works, that we could publish on https://getrector.com/blog under your name?
I think people should know about this gold :)

@calebdw

calebdw commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

Wow, I would love to! I've never written a blog post before though 😅, any tips?

@TomasVotruba

Copy link
Copy Markdown
Member

@calebdw Awesome, you'll be fine 👍 Looks like you have the skill already, based on clear descriptiosn of your PR 👍

You can kick off with this structure:

  • Why it's important?
  • How it works?
  • 2-3 examples
  • Try it :)

@calebdw

calebdw commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

Here's the blog post PR!

https://github.com/rectorphp/getrector-com/pull/3571

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants