Skip to content

Conversation

@neznaika0
Copy link
Contributor

Description
I need help.
I still don't understand how to work with generics. I can add simple hints, but any condition raises questions.

How do I specify here "If $which is class-string<Model> ? class-string<Model> : Model|object|string|null" and then update the hint for model($this->modelName)

The phpstan documentation is small. There is no description of most use cases. Did I miss something?

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@paulbalandan
Copy link
Member

Maybe this would help: https://phpstan.org/blog/generics-by-examples#function-accepts-any-string%2C-but-returns-object-of-the-same-type-if-it%E2%80%99s-a-class-string

@neznaika0 neznaika0 force-pushed the refactor/phpstan-model-argument-instanceof branch from 02138c8 to 2fa1eee Compare February 1, 2025 15:03
@neznaika0
Copy link
Contributor Author

RESTFul uses model() internally, so I think it's acceptable to add a Model layer to it.

if ($which !== null) {
$this->model = is_object($which) ? $which : null;
$this->modelName = is_object($which) ? null : $which;
$this->modelName = is_object($which) ? '' : $which;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change, any test for it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->modelName is always a string. Why is there null there? model() will throw an error if you pass null.
What test do you need? You even need to change the type $this->modelName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't agree, I'll just give it back.

@neznaika0 neznaika0 force-pushed the refactor/phpstan-model-argument-instanceof branch from 2fa1eee to 406c85e Compare March 7, 2025 08:03
@neznaika0 neznaika0 deleted the refactor/phpstan-model-argument-instanceof branch March 18, 2025 18:15
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

Successfully merging this pull request may close these issues.

3 participants