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

Give a hint using $this in static method. #6703

Closed
mhrownaghi opened this issue Nov 15, 2023 · 0 comments · Fixed by #6899
Closed

Give a hint using $this in static method. #6703

mhrownaghi opened this issue Nov 15, 2023 · 0 comments · Fixed by #6899
Assignees
Labels
kind:feature A feature request PHP [ci] enable extra PHP tests (php/php.editor)
Milestone

Comments

@mhrownaghi
Copy link

mhrownaghi commented Nov 15, 2023

Description

I may mistakenly use $this in a static method. It is expected that NetBeans will show this error in the code before that I run it in the PHP engine.

Use case/motivation

for example in the following code, there are a Fatal error.

<?php

class Person {
     private int $age = 18;
     
     public static function getAge(): int
     {
         return $this->age;
     }
}

echo Person::getAge();

but Netbeans do not find it.

Related issues

No response

Are you willing to submit a pull request?

No

@mhrownaghi mhrownaghi added kind:feature A feature request needs:triage Requires attention from one of the committers labels Nov 15, 2023
@junichi11 junichi11 added PHP [ci] enable extra PHP tests (php/php.editor) and removed needs:triage Requires attention from one of the committers labels Nov 16, 2023
@junichi11 junichi11 self-assigned this Nov 16, 2023
@junichi11 junichi11 added this to the NB21 milestone Nov 16, 2023
junichi11 added a commit to junichi11/netbeans that referenced this issue Jan 1, 2024
- apache#6703
- Show errors when `$this` is used in static context(static methods, static closures, static arrow functions)
- Add unit tests

Note:
Check only $this in TypeDeclarations because Frameworks may use `$this` in the global scope.
e.g. CakePHP framework can use `$this`(e.g. `$this->Html->link()`) in the global scope of view files.
(see: https://book.cakephp.org/5/en/views.html)

Example:
```php
class Example {
    private int $field = 1;

    public static function staticMethod(): void {
        $this->field; // error
    }

    public function method(): void {
        $staticClosure = static function() {
            var_dump($this); // error
        };
        $staticArrow = static fn() => $this; // error
        echo $this->field;
    }
}
```
@junichi11 junichi11 linked a pull request Jan 1, 2024 that will close this issue
junichi11 added a commit that referenced this issue Jan 1, 2024
…ntext

Add the `IncorrectStaticContextHintError` as a new hint #6703
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feature A feature request PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants