-   Notifications  
You must be signed in to change notification settings  - Fork 545
 
Support list<Foo> #79
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
   Closed  
     Closed  
 Changes from 1 commit
 Commits 
  Show all changes 
  14 commits   Select commit Hold shift + click to select a range 
 0789b6b  Resolve "list" type and "list<Foo>"" type 
  orklah 7e5a1e7  add tests for list and list<Foo> notations 
  orklah 02467b0  Revert "add tests for list and list<Foo> notations" 
  orklah 1ad0eb0  add new tests. Can't run them locally, will check on CI 
  orklah 5894535  Merge branch 'master' into list-type-implement 
  orklah 1e01a22  fix CS (broken by resolving conflict on github...) 
  orklah a77904e  fixed typo 
  orklah 965928b  fix indent 
  orklah 0734fc4  delete irrelevant tests, fix other tests 
  orklah f1d3e19  Merge branch 'master' into list-type-implement 
  orklah bd96c99  fix tests 
  orklah 36548d6  adding direct assertion to ensure the type is correct before modifica… 
  orklah 13d50c0  messed up phpdoc 
  orklah 2eb29a6  change type notation in assert 
  orklah File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
 add new tests. Can't run them locally, will check on CI 
 - Loading branch information
 
   commit 1ad0eb02b7f99f2abc5e122db7c6783112819ba5  
 There are no files selected for viewing
   This file contains hidden or 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 hidden or 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,110 @@ | ||
| <?php | ||
|   |  ||
| namespace ListType; | ||
|   |  ||
| use function PHPStan\Analyser\assertType; | ||
|   |  ||
| class Foo | ||
| { | ||
| public function withoutGenerics(): void | ||
| { | ||
| /** @var list $list */ | ||
| $list = []; | ||
| $list[] = '1'; | ||
| $list[] = true; | ||
| $list[] = new \stdClass(); | ||
| assertType('array<int, mixed>', $list); | ||
| } | ||
|   |  ||
|   |  ||
| public function withMixedType(): void | ||
| { | ||
| /** @var list<mixed> $list */ | ||
| $list = []; | ||
| $list[] = '1'; | ||
| $list[] = true; | ||
| $list[] = new \stdClass(); | ||
| assertType('array<int, mixed>', $list); | ||
| } | ||
|   |  ||
| public function withObjectType(): void | ||
| { | ||
| /** @var list<\DateTime> $list */ | ||
| $list = []; | ||
| $list[] = new \DateTime(); | ||
| assertType('array<int, \DateTime>', $list); | ||
| } | ||
|   |  ||
| public function withObjectTypeWrongContent(): void | ||
| { | ||
| /** @var list<\DateTime> $list */ | ||
| $list = []; | ||
| $list[] = new \DateTime(); | ||
| $list[] = false; | ||
| assertType('*error*', $list);//this should fail. Fix type | ||
| } | ||
|   |  ||
| public function withScalarTypeWrongContent(): void | ||
| { | ||
| /** @var list<scalar> $list */ | ||
| $list = []; | ||
| $list[] = '1'; | ||
| $list[] = true; | ||
| $list[] = new \DateTime(); | ||
| assertType('*error*', $list);//this should fail. Fix type | ||
| } | ||
|   |  ||
| /** @return list<scalar> */ | ||
| public function withScalarGoodContent(): void | ||
| { | ||
| /** @var list<scalar> $list */ | ||
| $list = []; | ||
| $list[] = '1'; | ||
| $list[] = true; | ||
| assertType('array<int, scalar>', $list); | ||
| } | ||
|   |  ||
| public function withStringKey(): void | ||
| { | ||
| /** @var list $list */ | ||
| $list = []; | ||
| $list[] = '1'; | ||
| $list['a'] = true; | ||
| assertType('*error*', $list);//this should fail. Fix type | ||
| } | ||
|   |  ||
| public function withNumericKey(): void | ||
| { | ||
| /** @var list $list */ | ||
| $list = []; | ||
| $list[] = '1'; | ||
| $list['1'] = true; | ||
| assertType('array<int, scalar>', $list); | ||
| } | ||
|   |  ||
| public function withWrongGenericsNumber(): void | ||
| { | ||
| /** @var list<int, mixed> $list */ | ||
| $list = []; | ||
| $list[] = '1'; | ||
| $list[] = true; | ||
| assertType('*error*', $list);//this should fail. Fix type | ||
| } | ||
|   |  ||
| public function withFullListFunctionality(): void | ||
| { | ||
| // These won't output errors for now but should when list type will be fully implemented | ||
| /** @var list $list */ | ||
| $list = []; | ||
| $list[] = '1'; | ||
| $list[] = '2'; | ||
| unset($list[0]);//break list behaviour | ||
| assertType('array<int, mixed>', $list); | ||
|   |  ||
| /** @var list $list2 */ | ||
| $list2 = []; | ||
| $list2[2] = '1';//Most likely to create a gap in indexes | ||
| assertType('array<int, scalar>', $list2); | ||
| } | ||
|   |  ||
| } | ||
 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.
You should at least once typehint the type as method parameter and assert it right at the beginning of the body. If you assert the variable only after some changes (array appending), you have no guarantees about the state right after
$list = [];.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.
Ok, I added some test cases with direct assertions. It should be ok