Git Product home page Git Product logo

Comments (7)

odan avatar odan commented on May 23, 2024 1

It looks like that the default value of a ?type property is not null, it is still uninitialized.

https://3v4l.org/oanOO

Setting null directly works.

public ?Bar $bar = null;

https://3v4l.org/KqLoA

from phpstan-strict-rules.

ondrejmirtes avatar ondrejmirtes commented on May 23, 2024 1

See this article: https://phpstan.org/blog/detecting-unused-private-properties-methods-constants

from phpstan-strict-rules.

rask avatar rask commented on May 23, 2024

Clarification: when typed as ?type the property is defaulted to null, which is not the same as uninitialized, and which should not trigger the error.

from phpstan-strict-rules.

ondrejmirtes avatar ondrejmirtes commented on May 23, 2024

Hi, this is difficult to get right, because PHP specifically allows the property to be uninitialized until used for the first time. So the property doesn't have to be initialized in the constructor... Any opinion how to tackle this?

from phpstan-strict-rules.

rask avatar rask commented on May 23, 2024

Is the following possible:

  1. When loading a class, collect all typed properties which are not nullable or do not contain a default value.
  2. When analysing the __construct() method, see which of those variables are receiving a value.

Or would this require extensive juggling inside the analyzer code? Of course this could end up deep when we start to factor in calling other class methods that do the property setting, or if some external entity is used to set the value.

from phpstan-strict-rules.

spawnia avatar spawnia commented on May 23, 2024

PHP specifically allows the property to be uninitialized until used for the first time.

That's right, and for some users that might be a fine way to program.

So the property doesn't have to be initialized in the constructor.

That's the point where strict rules could apply and require initialization is completed during construction. Properties that are not lazily initialized might still be allowed, but have to be checked before access through isset().

from phpstan-strict-rules.

github-actions avatar github-actions commented on May 23, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from phpstan-strict-rules.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.