| 1 |  |  | <?php namespace Anomaly\Streams\Platform\Ui\Table\Component\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Anomaly\Streams\Platform\Ui\Table\TableBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Class ActionNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @link   http://pyrocms.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author PyroCMS, Inc. <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author Ryan Thompson <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class ActionNormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * Normalize action input. | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @param TableBuilder $builder | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function normalize(TableBuilder $builder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $actions = $builder->getActions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $prefix  = $builder->getTableOption('prefix'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         foreach ($actions as $slug => &$action) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             $action = $this->process($prefix, $slug, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $builder->setActions($actions); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Process the action. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @param $prefix | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @param $slug | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @param $action | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function process($prefix, $slug, $action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |          * If the slug is numeric and the action is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |          * a string then treat the string as both the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |          * action and the slug. This is OK as long as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |          * there are not multiple instances of this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |          * input using the same action which is not likely. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |          */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 49 |  | View Code Duplication |         if (is_numeric($slug) && is_string($action)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $action = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 'slug'   => $action, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 'action' => $action, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |          * If the slug is NOT numeric and the action is a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |          * string then use the slug as the slug and the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |          * action as the action. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |          */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 61 |  | View Code Duplication |         if (!is_numeric($slug) && is_string($action)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $action = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'slug'   => $slug, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 'action' => $action, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |          * If the slug is not numeric and the action is an | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |          * array without a slug then use the slug for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |          * the slug for the action. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |          */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 73 |  | View Code Duplication |         if (is_array($action) && !isset($action['slug']) && !is_numeric($slug)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $action['slug'] = $slug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |          * If the slug is not numeric and the action is an | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |          * array without a action then use the slug for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |          * the action for the action. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |          */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 82 |  | View Code Duplication |         if (is_array($action) && !isset($action['action']) && !is_numeric($slug)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $action['action'] = $slug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |          * Make sure the attributes array is set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $action['attributes'] = array_get($action, 'attributes', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |          * Move all data-* keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |          * to attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         foreach ($action as $attribute => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             if (str_is('data-*', $attribute)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 array_set($action, 'attributes.' . $attribute, array_pull($action, $attribute)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |          * If the HREF is present outside of the attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |          * then pull it and put it in the attributes array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if (isset($action['url'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $action['attributes']['url'] = array_pull($action, 'url'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |          * Set defaults as expected for actions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $action['size']     = array_get($action, 'small', 'sm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $action['disabled'] = array_get($action, 'disabled', array_get($action, 'toggle', true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $action['attributes']['name']  = $prefix . 'action'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $action['attributes']['value'] = $action['slug']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         // If not toggle add the ignore attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         if (array_get($action, 'toggle', true) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $action['attributes']['data-ignore'] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 123 |  | View Code Duplication |         if (isset($action['dropdown'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             foreach ($action['dropdown'] as $key => &$dropdown) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 $dropdown = $this->process($prefix, $key, $dropdown); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $action; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  | 
            
                        
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.