Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phan fails to warn about type mismatches for object with __toString **declared** without strict types #4827

Open
TysonAndre opened this issue Dec 26, 2023 · 0 comments

Comments

@TysonAndre
Copy link
Member

This is because of the call to addAdditionalType

// src/Phan/Parse/ParseVisitor.php
//    public function visitMethod(Node $node): Context

        } elseif ('__tostring' === $method_name_lower) {
            if (!$this->context->isStrictTypes()) {
                $class->addAdditionalType(StringType::instance(false));
            }
            // In PHP 8 and later having a __toString method automatically adds the Stringable interface, #4476
            if (Config::get_closest_minimum_target_php_version_id() >= 80000) {
                // @phan-suppress-next-line PhanThrowTypeAbsentForCall should not happen, built in type
                $class->addAdditionalType(Type::fromFullyQualifiedString('\Stringable'));
            }
        }
<?php // b.php without strict_types
class HasToString { public function __toString() { return 'x'; }}
<?php // a.php
declare(strict_types=1);
require_once __DIR__ . '/b.php';
throw new Exception(new HasToString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant