Document that views_get_current_view() can return NULL

Created on 9 March 2022, about 3 years ago
Updated 3 February 2023, over 2 years ago

Problem/Motivation

phpstan complains that if ($view = views_get_current_view()) { } is an always true condition.
That's because the docblock for views_get_current_view() does not declare that it can return NULL.

Steps to reproduce

Run drupal-check -a on this code :

if ($view = views_get_current_view()) {

}

Proposed resolution

The function should have a return type of ?ViewExecutable.

πŸ“Œ Task
Status

Needs work

Version

9.5

Component
ViewsΒ  β†’

Last updated about 7 hours ago

Created by

πŸ‡«πŸ‡·France prudloff Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    I added the code from the IS into ClaroPreRender.php and ran drupal-check on the folder.
    Confirmed the issue is there

    but the fix in the MR did not make it go away

  • Pipeline finished with Success
    2 months ago
    Total: 372s
    #436193
  • Status changed to Needs review 2 months ago
  • πŸ‡«πŸ‡·France prudloff Lille

    @smustgrave I tried reproducing with your method.
    I am adding this change:

    diff --git a/core/themes/claro/src/ClaroPreRender.php b/core/themes/claro/src/ClaroPreRender.php
    index 82f73ac9fa3..3b566998894 100644
    --- a/core/themes/claro/src/ClaroPreRender.php
    +++ b/core/themes/claro/src/ClaroPreRender.php
    @@ -16,6 +16,10 @@ class ClaroPreRender implements TrustedCallbackInterface {
        * Prerender callback for managed_file.
        */
       public static function managedFile($element) {
    +    if ($view = views_get_current_view()) {
    +
    +    }
    +
         if (!empty($element['remove_button']) && is_array($element['remove_button'])) {
           $element['remove_button']['#attributes']['class'][] = 'button--extrasmall';
           $element['remove_button']['#attributes']['class'][] = 'remove-button';
    
    

    Then I am calling ./vendor/bin/phpstan analyze core/themes/claro/src/ClaroPreRender.php --level 5.

    Without the patch:

     ------ -------------------------------------------------------------------------------------------------------------------------------------------- 
      Line   ClaroPreRender.php                                                                                                                          
     ------ -------------------------------------------------------------------------------------------------------------------------------------------- 
      19     If condition is always true.                                                                                                                
             πŸͺͺ  if.alwaysTrue                                                                                                                           
             πŸ’‘ Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your phpstan.neon.  
     ------ -------------------------------------------------------------------------------------------------------------------------------------------- 
    

    With the patch:

                                                                                                                            
     [OK] No errors                                                                                                         
                         
    
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks for checking.

    If false can be returned can the description be updated as well when false could be returned. 100% agree on the NULL and it's already there.

    Will keep an eye out for this one to come back.

    If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

  • πŸ‡«πŸ‡·France prudloff Lille

    ViewExecutable::postExecute() does this:

    views_set_current_view($old_view ?? FALSE);
    

    Now that I think about it, wouldn't it be cleaner to change it like this:

    views_set_current_view($old_view);
    

    And only document ViewExecutable|null as allowed values?

    I feel like null has a clearer meaning that false here.

  • Pipeline finished with Success
    2 months ago
    Total: 1042s
    #440977
  • πŸ‡«πŸ‡·France prudloff Lille

    OK I see why ViewExecutable::postExecute() calls views_set_current_view() with FALSE: calling it with null does not change the cached value, so calling it with false is a way to explicitly remove the cached value.
    So I will just document the current behavior.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Can you drop the return types, think adding those requires more work for BC. Fine to self RTBC after that

    If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

  • Pipeline finished with Success
    2 months ago
    Total: 654s
    #441024
  • πŸ‡«πŸ‡·France prudloff Lille

    I removed the return types.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Committed/pushed to 11.x, thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024