- 
                Notifications
    You must be signed in to change notification settings 
- Fork 545
[ReturnTypeExtension] add test case for narrow typed json_decode #993
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
          
     Merged
      
      
            ondrejmirtes
  merged 21 commits into
  phpstan:1.6.x
from
TomasVotruba:tv-json-decode-narrow-type
  
      
      
   
  May 4, 2022 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            21 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      c4e495a
              
                add test case for narrow typed json_decode
              
              
                TomasVotruba 38ca032
              
                Extend JsonThrowOnErrorDynamicReturnTypeExtension to detect knonw typ…
              
              
                TomasVotruba 6d4e3b3
              
                update type in LegacyNodeScopeResolverTest
              
              
                TomasVotruba 581f51a
              
                return mixed type
              
              
                TomasVotruba d665722
              
                add json_decode false
              
              
                TomasVotruba b2b979b
              
                check for JSON_OBJECT_AS_ARRAY, in case of null and array
              
              
                TomasVotruba 665e9da
              
                add test case for invalid json string
              
              
                TomasVotruba 66e3aa0
              
                add test for multiple flags
              
              
                TomasVotruba 1a124ea
              
                decopule type resolution to static
              
              
                TomasVotruba bea4f29
              
                check if JSON_THROW_ON_ERROR exists before infer
              
              
                TomasVotruba 8748e9c
              
                [ci] add json to composer require checker json
              
              
                TomasVotruba 43eb542
              
                use bitwiseFlagAnalyser
              
              
                TomasVotruba 99debb9
              
                fixup! use bitwiseFlagAnalyser
              
              
                TomasVotruba d707038
              
                Update src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php
              
              
                TomasVotruba 0255425
              
                Update src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php
              
              
                TomasVotruba d586c6e
              
                fixup! fixup! use bitwiseFlagAnalyser
              
              
                TomasVotruba 98abc6f
              
                fixup! fixup! fixup! use bitwiseFlagAnalyser
              
              
                TomasVotruba 8a841fe
              
                fixup! fixup! fixup! fixup! use bitwiseFlagAnalyser
              
              
                TomasVotruba 3ad68aa
              
                Simplify isForceArrayWithoutStdClass
              
              
                herndlm 741abd2
              
                Simplify fallback type handling
              
              
                herndlm 7d306fb
              
                Remove unneeded var in narrowTypeForJsonDecode
              
              
                herndlm 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
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
    
  
  
    
              
  
    
      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,17 @@ | ||
| <?php | ||
|  | ||
| namespace Analyser\JsonDecode; | ||
|  | ||
| use function PHPStan\Testing\assertType; | ||
|  | ||
| $value = json_decode('{"key"}'); | ||
| assertType('null', $value); | ||
|  | ||
| $value = json_decode('{"key"}', true); | ||
| assertType('null', $value); | ||
|  | ||
| $value = json_decode('{"key"}', null); | ||
| assertType('null', $value); | ||
|  | ||
| $value = json_decode('{"key"}', false); | ||
| assertType('null', $value); | 
        
          
          
            33 changes: 33 additions & 0 deletions
          
          33 
        
  tests/PHPStan/Analyser/data/json-decode/json_object_as_array.php
  
  
      
      
   
        
      
      
    
  
    
      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,33 @@ | ||
| <?php | ||
|  | ||
| namespace Analyser\JsonDecode; | ||
|  | ||
| use function PHPStan\Testing\assertType; | ||
|  | ||
| // @see https://3v4l.org/YFlHF | ||
| function ($mixed) { | ||
| $value = json_decode($mixed, null, 512, JSON_OBJECT_AS_ARRAY); | ||
| assertType('mixed~stdClass', $value); | ||
| }; | ||
|  | ||
| function ($mixed) { | ||
| $flagsAsVariable = JSON_OBJECT_AS_ARRAY; | ||
|  | ||
| $value = json_decode($mixed, null, 512, $flagsAsVariable); | ||
| assertType('mixed~stdClass', $value); | ||
| }; | ||
|  | ||
| function ($mixed) { | ||
| $value = json_decode($mixed, null, 512, JSON_OBJECT_AS_ARRAY | JSON_BIGINT_AS_STRING); | ||
| assertType('mixed~stdClass', $value); | ||
| }; | ||
|  | ||
| function ($mixed) { | ||
| $value = json_decode($mixed, null); | ||
| assertType('mixed', $value); | ||
| }; | ||
|  | ||
| function ($mixed, $unknownFlags) { | ||
| $value = json_decode($mixed, null, 512, $unknownFlags); | ||
| assertType('mixed', $value); | ||
| }; | 
  
    
      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,34 @@ | ||
| <?php | ||
|  | ||
| namespace Analyser\JsonDecode; | ||
|  | ||
| use function PHPStan\Testing\assertType; | ||
|  | ||
| $value = json_decode('true'); | ||
| assertType('true', $value); | ||
|  | ||
| $value = json_decode('1'); | ||
| assertType('1', $value); | ||
|  | ||
| $value = json_decode('1.5'); | ||
| assertType('1.5', $value); | ||
|  | ||
| $value = json_decode('false'); | ||
| assertType('false', $value); | ||
|  | ||
| $value = json_decode('{}'); | ||
| assertType('stdClass', $value); | ||
|  | ||
| $value = json_decode('[1, 2, 3]'); | ||
| assertType('array{1, 2, 3}', $value); | ||
|  | ||
|  | ||
| function ($mixed) { | ||
| $value = json_decode($mixed); | ||
| assertType('mixed', $value); | ||
| }; | ||
|  | ||
| function ($mixed) { | ||
| $value = json_decode($mixed, false); | ||
| assertType('mixed', $value); | ||
| }; | 
        
          
          
            28 changes: 28 additions & 0 deletions
          
          28 
        
  tests/PHPStan/Analyser/data/json-decode/narrow_type_with_force_array.php
  
  
      
      
   
        
      
      
    
  
    
      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,28 @@ | ||
| <?php | ||
|  | ||
| namespace Analyser\JsonDecode; | ||
|  | ||
| use function PHPStan\Testing\assertType; | ||
|  | ||
| $value = json_decode('true', true); | ||
| assertType('true', $value); | ||
|  | ||
| $value = json_decode('1', true); | ||
| assertType('1', $value); | ||
|  | ||
| $value = json_decode('1.5', true); | ||
| assertType('1.5', $value); | ||
|  | ||
| $value = json_decode('false', true); | ||
| assertType('false', $value); | ||
|  | ||
| $value = json_decode('{}', true); | ||
| assertType('array{}', $value); | ||
|  | ||
| $value = json_decode('[1, 2, 3]', true); | ||
| assertType('array{1, 2, 3}', $value); | ||
|         
                  TomasVotruba marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| function ($mixed) { | ||
| $value = json_decode($mixed, true); | ||
| assertType('mixed~stdClass', $value); | ||
| }; | ||
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.