Conditions | 22 |
Paths | 290 |
Total Lines | 139 |
Code Lines | 90 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
70 | public static function process_hooks() { |
||
71 | |||
72 | self::initialize(); |
||
73 | |||
74 | // If we have one, get the PHP files from it. |
||
75 | $template_files = self::get_files( '*.php', GLOB_MARK, self::$sensei_directory.'/templates/' ); |
||
76 | $template_files[] = self::$sensei_directory . '/includes/template-functions.php'; |
||
77 | |||
78 | $shortcode_files = self::get_files( '*.php', GLOB_MARK, self::$sensei_directory.'/includes/shortcodes/' ); |
||
79 | $widget_files = self::get_files( '*.php', GLOB_MARK, self::$sensei_directory.'/widgets/' ); |
||
80 | $admin_files = self::get_files( '*.php', GLOB_MARK, self::$sensei_directory.'/includes/admin/' ); |
||
81 | $class_files = self::get_files( '*.php', GLOB_MARK, self::$sensei_directory.'/includes/' ); |
||
82 | $other_files = array( |
||
83 | self::$sensei_directory.'/woothemes-sensei.php' |
||
84 | ); |
||
85 | |||
86 | self::$files_to_scan = array( |
||
87 | 'Template Hooks' => $template_files, |
||
88 | 'Shortcode Hooks' => $shortcode_files, |
||
89 | 'Widget Hooks' => $widget_files, |
||
90 | 'Class Hooks' => $class_files, |
||
91 | 'Admin Hooks' => $admin_files, |
||
92 | 'Other Hooks' => $other_files, |
||
93 | ); |
||
94 | |||
95 | $scanned = array(); |
||
96 | |||
97 | ob_start(); |
||
98 | |||
99 | echo '<div id="content">'; |
||
100 | echo '<h1>Action and Filter Hook Reference</h1>'; |
||
101 | echo '<div class="description"><p>The following is a full list of actions and filters found in Sensei.</p></div>'; |
||
102 | |||
103 | foreach ( self::$files_to_scan as $heading => $files ) { |
||
104 | self::$custom_hooks_found = array(); |
||
105 | |||
106 | foreach ( $files as $f ) { |
||
107 | self::$current_file = basename( $f ); |
||
108 | |||
109 | if ( in_array( self::$current_file, $scanned ) ) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | $scanned[] = self::$current_file; |
||
114 | |||
115 | $tokens = token_get_all( file_get_contents( $f ) ); |
||
116 | $token_type = false; |
||
117 | $current_class = ''; |
||
118 | $current_function = ''; |
||
119 | |||
120 | foreach ( $tokens as $index => $token ) { |
||
121 | if ( is_array( $token ) ) { |
||
122 | if ( $token[0] == T_CLASS ) { |
||
123 | $token_type = 'class'; |
||
124 | } elseif ( $token[0] == T_FUNCTION ) { |
||
125 | $token_type = 'function'; |
||
126 | } elseif ( $token[1] === 'do_action' ) { |
||
127 | $token_type = 'action'; |
||
128 | } elseif ( $token[1] === 'apply_filters' ) { |
||
129 | $token_type = 'filter'; |
||
130 | } elseif ( $token_type && ! empty( trim( $token[1] ) ) ) { |
||
131 | switch ( $token_type ) { |
||
132 | case 'class' : |
||
133 | $current_class = $token[1]; |
||
134 | break; |
||
135 | case 'function' : |
||
136 | $current_function = $token[1]; |
||
137 | break; |
||
138 | case 'filter' : |
||
139 | case 'action' : |
||
140 | $hook = trim( $token[1], "'" ); |
||
141 | if ( isset( self::$custom_hooks_found[ $hook ] ) ) { |
||
142 | self::$custom_hooks_found[ $hook ]['file'][] = self::$current_file; |
||
143 | } else { |
||
144 | self::$custom_hooks_found[ $hook ] = array( |
||
145 | 'line' => $token[2], |
||
146 | 'class' => $current_class, |
||
147 | 'function' => $current_function, |
||
148 | 'file' => array( self::$current_file ), |
||
149 | 'type' => $token_type |
||
150 | ); |
||
151 | } |
||
152 | break; |
||
153 | } |
||
154 | $token_type = false; |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | foreach ( self::$custom_hooks_found as $hook => $details ) { |
||
161 | if ( ! strstr( $hook, 'sensei' ) ) { |
||
162 | unset( self::$custom_hooks_found[ $hook ] ); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | ksort( self::$custom_hooks_found ); |
||
167 | |||
168 | if ( ! empty( self::$custom_hooks_found ) ) { |
||
169 | echo '<h2>' . $heading . '</h2>'; |
||
170 | |||
171 | echo '<table class="summary"><thead><tr><th>Hook</th><th>Type</th><th>File(s)</th></tr></thead><tbody>'; |
||
172 | |||
173 | foreach ( self::$custom_hooks_found as $hook => $details ) { |
||
174 | echo '<tr> |
||
175 | <td>' . self::get_hook_link( $hook, $details ) . '</td> |
||
176 | <td>' . $details['type'] . '</td> |
||
177 | <td>' . implode( ', ', array_unique( $details['file'] ) ) . '</td> |
||
178 | </tr>' . "\n"; |
||
179 | } |
||
180 | |||
181 | echo '</tbody></table>'; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | echo '</div><div id="footer">'; |
||
186 | |||
187 | |||
188 | |||
189 | // change to the ouput directory before operating on the files |
||
190 | chdir( self::$docs_output_directory ); |
||
191 | |||
192 | $html = file_get_contents( 'index.html' ); |
||
193 | $header = current( explode( '<div id="content">', $html ) ); |
||
194 | $header = str_replace( '<li class="active">', '<li>', $header ); |
||
195 | $header = str_replace( '<li class="hooks">', '<li class="active">', $header ); |
||
196 | $footer = end( explode( '<div id="footer">', $html ) ); |
||
197 | |||
198 | // delete old hook-docs file |
||
199 | if( file_exists( self::$put_file ) ){ |
||
200 | |||
201 | unlink( self::$put_file ); |
||
202 | |||
203 | } |
||
204 | |||
205 | file_put_contents( self::$put_file , $header . ob_get_clean() . $footer ); |
||
206 | |||
207 | echo "Hook docs generated :)\n"; |
||
208 | } |
||
209 | } |
||
211 | Sensei_HookFinder::process_hooks(); |
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.