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
PropertyNotSetInConstructor - when trait defined same property in different case #4393
Comments
I found these snippets: https://psalm.dev/r/69f9c98ac1<?php
interface ViewHandlerInterface {}
trait ControllerTrait {
private ViewHandlerInterface $viewhandler;
}
class Controller {
use ControllerTrait;
private $viewHandler;
public function __construct(ViewHandlerInterface $viewHandler) {
$this->viewHandler = $viewHandler;
}
}
|
Another example https://psalm.dev/r/8c91ef717d |
I found these snippets: https://psalm.dev/r/8c91ef717d<?php
interface ViewHandlerInterface {}
trait ControllerTrait {
private ViewHandlerInterface $viewhandler;
public function setViewHandler(ViewHandlerInterface $viewhandler): void {
$this->viewhandler = $viewhandler;
}
}
class Controller {
use ControllerTrait;
public function __construct(ViewHandlerInterface $viewHandler) {
$this->setViewHandler($viewHandler);
}
}
|
The latter is valid and can be fixed by making |
I found these snippets: https://psalm.dev/r/3dcfb7762c<?php
interface ViewHandlerInterface {}
trait ControllerTrait {
private ViewHandlerInterface $viewhandler;
public final function setViewHandler(ViewHandlerInterface $viewhandler): void {
$this->viewhandler = $viewhandler;
}
}
class Controller {
use ControllerTrait;
public function __construct(ViewHandlerInterface $viewHandler) {
$this->setViewHandler($viewHandler);
}
}
|
@weirdan It's a vendors code. I cannot do it. It's still weird that final is required. |
I found these snippets: https://psalm.dev/r/3dcfb7762c<?php
interface ViewHandlerInterface {}
trait ControllerTrait {
private ViewHandlerInterface $viewhandler;
public final function setViewHandler(ViewHandlerInterface $viewhandler): void {
$this->viewhandler = $viewhandler;
}
}
class Controller {
use ControllerTrait;
public function __construct(ViewHandlerInterface $viewHandler) {
$this->setViewHandler($viewHandler);
}
}
|
Not really. If it's neither |
So by making my class final this issue should go away? |
I found these snippets: https://psalm.dev/r/e802436284<?php
interface ViewHandlerInterface {}
trait ControllerTrait {
private ViewHandlerInterface $viewhandler;
public function setViewHandler(ViewHandlerInterface $viewhandler): void {
$this->viewhandler = $viewhandler;
}
}
final class Controller {
use ControllerTrait;
public function __construct(ViewHandlerInterface $viewHandler) {
$this->setViewHandler($viewHandler);
}
}
|
It should, and it does work for inline methods: https://psalm.dev/r/c47bf2af78 , but apparently it fails to consider final when calling a method from trait. |
I found these snippets: https://psalm.dev/r/c47bf2af78<?php
interface ViewHandlerInterface {}
final class Controller {
private ViewHandlerInterface $viewhandler;
public function setViewHandler(ViewHandlerInterface $viewhandler): void {
$this->viewhandler = $viewhandler;
}
public function __construct(ViewHandlerInterface $viewHandler) {
$this->setViewHandler($viewHandler);
}
}
|
it's just confused about inherited properties from traits, because that property appears twice. Should be a very simple fix. |
It actually doesn't. There are two different properties involved: Edit: this refers to the original case, not the one discussed later. |
ah ok, sorry so it's the difference between <?php
interface A {}
trait ATrait {
private A $a;
public function setA(A $a): void {
$this->a = $a;
}
}
final class C {
use ATrait;
public function __construct(A $a) {
$this->setA($a);
}
} and <?php
interface A {}
trait ATrait {
private A $a;
public final function setA(A $a): void {
$this->a = $a;
}
}
final class C {
use ATrait;
public function __construct(A $a) {
$this->setA($a);
}
} |
Is #6087 the same issue? |
It looks like this issue is actually just about improving the error message. Psalm is correct here that |
Example: https://psalm.dev/r/69f9c98ac1
While it looks like a valid case, the error message could be improved and say that property is defined in trait
The text was updated successfully, but these errors were encountered: