Re: Warning for "confusable" types
| From: | Larry Garfield | Date: | Fri, 11 Oct 2019 19:21:50 +0000 |
| Subject: | Re: Warning for "confusable" types | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107525@lists.php.net to get a copy of this message | ||
On Fri, Oct 11, 2019, at 1:46 PM, Bishop Bettini wrote:
> On Fri, Oct 11, 2019 at 1:47 PM Larry Garfield <larry@garfieldtech.com>
> wrote:
>
> > On Fri, Oct 11, 2019, at 8:54 AM, Nikita Popov wrote:
> > > Hi internals,
> > >
> > > Something I've seen play out a couple of times: Newbies try to use
> > > something like "integer" or "resource" as a type, and then get a
> > confusing
> > > error message along the lines of "must be an instance of resource,
> > resource
> > > given".
> > >
> > > I would like to throw a compiler warning in this case, that looks as one
> > of
> > > the following:
> > >
> > > > Warning: "integer" will be interpreted as a class type. Did you mean
> > > "int"? Use qualified name or "use" to suppress this warning
> > >
> > > > Warning: "resource" is not a supported builtin type and will be
> > > interpreted as a class type. Use qualified name or "use" to suppress this
> > > warning
> > >
> > > This warning only triggers if the type is lowercase (integer but not
> > > Integer), is unqualified (integer but not \integer) and is not imported
> > > (there is no "use integer"). This provides multiple ways to avoid the
> > > warning for code that does legitimately want to use lowercase "integer"
> > as
> > > a class type.
> > >
> > > Implementation: https://github.com/php/php-src/pull/4815
> > >
> > >
> > > Thoughts?
> > >
> > > Nikita
> >
> > Can you clarify where exactly "compiler warning" would be displayed? Is
> > that an E_WARNING? How would I as a user see a message when I write
> >
> > function foo(integer $a) { ... }
> >
>
> It's an E_COMPILE_WARNING, and arises when the compiler sees you're type
> hinting for a class, but no such class loaded. This checks that given class
> against a known list of "probable mistakes" and provides guidance. Eg, I
> typed "integer $a" when I mean "int $a", and don't understand the
> current
> compiler warning: "Argument 1 passed to foo must be an instance of integer,
> int given"
>
> See also https://stackoverflow.com/a/40638544/2908724
OK, so it's just taking an existing error message and customizing it in cases where we have a
good idea of what the user got wrong? That's a very +1 in my book.
--Larry Garfield