| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | require __DIR__.'/../vendor/autoload.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SmsaSDK\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | $methodsAndResponses = get_methods_and_responses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | $trait = <<<'EOD' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace SmsaSDK; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | trait MethodsRedirector { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | EOD; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | foreach ($methodsAndResponses as $method => $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     $trait .= "\n\r".build_method($method, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | $trait .= "\n\r}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | file_put_contents(__DIR__.'/../src/MethodsRedirector.php', $trait); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | function build_method($method, $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     $docBlock = "/**\n\r"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     $rawMethod = "public static function $method("; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     $innerMethodTemplate = file_get_contents(__DIR__.'/stubs/MethodsRedirectorInnerMethod.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     $innerMethodTemplate = str_replace('%method', $method, $innerMethodTemplate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     $innerMethod = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     $reflection = new ReflectionClass('SmsaSDK\\Methods\\'.$method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     $params = $reflection->getConstructor()->getParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     foreach ($params as $param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $docBlock .= '* @param $'.$param->name."\n\r"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $rawMethod .= '$'.$param->name.' = null, '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $innerMethod .= "if(!is_null(\$$param->name)) { \$arguments['$param->name'] = \$$param->name; } \n\r"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     $docBlock .= "* @return \SmsaSDK\Methods\\$response\n\r"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     $docBlock .= '*/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     $innerMethodTemplate = str_replace('%innerMethod', $innerMethod, $innerMethodTemplate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     $rawMethod = substr($rawMethod, 0, -2); // remove the last comma and space ", " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     $rawMethod .= ") { $innerMethodTemplate }"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     return $docBlock."\n\r".$rawMethod; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 52 |  |  | function get_methods_and_responses() | 
            
                                                        
            
                                    
            
            
                | 53 |  |  | { | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |     $wsdlFile = file_get_contents(Config::get('wsdl_file_path')); | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     $responseRegex = '/"([^"]+)Response"/'; | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     preg_match_all($responseRegex, $wsdlFile, $responseMethods); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     $responseMethods = array_filter( | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $responseMethods[1], function ($method) { | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             return substr($method, 0, 4) != 'tns:'; | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     ); | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |     asort($responseMethods); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     $responseMethods = array_values(array_unique($responseMethods)); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     $responseMethodsCopy = []; | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     array_walk( | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         $responseMethods, function ($method) use (&$responseMethodsCopy) { | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |             $responseMethodsCopy[$method] = $method.'Response'; | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |     ); | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |     return $responseMethodsCopy; | 
            
                                                        
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.