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
feat(types): add properties-of<T> type #7359
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
30fac90
feat(types): add properties-of<T> type
Patrick-Remy 9c6deac
feat: expand properties-of to keyed array
Patrick-Remy 6e783d8
feat: ignore static properties in properties-of<T>
Patrick-Remy d81c74e
style: fix linting issues and failing tests
Patrick-Remy 5e74445
docs: add utility types chapter
Patrick-Remy be49037
feat: add template support to properties-of<T>
Patrick-Remy 8ebc9b5
docs: describe template support of properties-of<T>
Patrick-Remy 51f3b98
docs: add properties-of type to plugins type system
Patrick-Remy 95de0c8
docs: add key-of and value-of to utility types chapter
Patrick-Remy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Utility types | ||
|
||
Psalm supports some _magical_ utility types that brings superpower to the PHP type system. | ||
|
||
## `properties-of<T>` | ||
|
||
This collection of _utility types_ construct a keyed-array, with the names of non-static properties of a class as keys, | ||
and their respective types as values. This can be useful if you need to convert objects into arrays. | ||
|
||
```php | ||
class A { | ||
public string $foo; | ||
public int $bar; | ||
|
||
/** | ||
* @return properties-of<self> | ||
*/ | ||
public function asArray(): array { | ||
return [ | ||
'foo' => $this->foo, | ||
'bar' => $this->bar, | ||
]; | ||
} | ||
|
||
/** | ||
* @return list<key-of<properties-of<self>>> | ||
*/ | ||
public function attributeNames(): array { | ||
return ['foo', 'bar'] | ||
} | ||
} | ||
``` | ||
|
||
### Variants | ||
|
||
Note that `properties-of<T>` will return **all non-static** properties. There are the following subtypes to pick only | ||
properties with a certain visibility: | ||
- `public-properties-of<T>` | ||
- `protected-properties-of<T>` | ||
- `private-properties-of<T>` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where are
key-of
andvalue-of
documented? Maybe they should be added here? MaybeSomeClass::CONST_*
as well? Probably others?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add these as well in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added
key-of
andvalue-of
,SomeClass::CONST_*
also could need some documentation, but I think it's not a utility type if you look at typescripts definition (https://www.typescriptlang.org/docs/handbook/utility-types.html)