Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like csrfProtector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use csrfProtector, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 25 | class csrfProtector  | 
            ||
| 26 |     { | 
            ||
| 27 | /*  | 
            ||
| 28 | * Variable: $cookieExpiryTime  | 
            ||
| 29 | * expiry time for cookie  | 
            ||
| 30 | * @var int  | 
            ||
| 31 | */  | 
            ||
| 32 | public static $cookieExpiryTime = 1800; //30 minutes  | 
            ||
| 33 | |||
| 34 | /*  | 
            ||
| 35 | * Variable: $isSameOrigin  | 
            ||
| 36 | * flag for cross origin/same origin request  | 
            ||
| 37 | * @var bool  | 
            ||
| 38 | */  | 
            ||
| 39 | private static $isSameOrigin = true;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 40 | |||
| 41 | /*  | 
            ||
| 42 | * Variable: $isValidHTML  | 
            ||
| 43 | * flag to check if output file is a valid HTML or not  | 
            ||
| 44 | * @var bool  | 
            ||
| 45 | */  | 
            ||
| 46 | private static $isValidHTML = false;  | 
            ||
| 47 | |||
| 48 | /*  | 
            ||
| 49 | * Variable: $requestType  | 
            ||
| 50 | * Varaible to store weather request type is post or get  | 
            ||
| 51 | * @var string  | 
            ||
| 52 | */  | 
            ||
| 53 | protected static $requestType = "GET";  | 
            ||
| 54 | |||
| 55 | /*  | 
            ||
| 56 | * Variable: $config  | 
            ||
| 57 | * config file for CSRFProtector  | 
            ||
| 58 | * @var int Array, length = 6  | 
            ||
| 59 | * Property: #1: failedAuthAction (int) => action to be taken in case autherisation fails  | 
            ||
| 60 | * Property: #2: logDirectory (string) => directory in which log will be saved  | 
            ||
| 61 | * Property: #3: customErrorMessage (string) => custom error message to be sent in case  | 
            ||
| 62 | * of failed authentication  | 
            ||
| 63 | * Property: #4: jsFile (string) => location of the CSRFProtector js file  | 
            ||
| 64 | * Property: #5: tokenLength (int) => default length of hash  | 
            ||
| 65 | * Property: #6: disabledJavascriptMessage (string) => error message if client's js is disabled  | 
            ||
| 66 | */  | 
            ||
| 67 | public static $config = array();  | 
            ||
| 68 | |||
| 69 | /*  | 
            ||
| 70 | * Variable: $requiredConfigurations  | 
            ||
| 71 | * Contains list of those parameters that are required to be there  | 
            ||
| 72 | * in config file for csrfp to work  | 
            ||
| 73 | */  | 
            ||
| 74 |         public static $requiredConfigurations = array('logDirectory', 'failedAuthAction', 'jsPath', 'jsUrl', 'tokenLength'); | 
            ||
| 75 | |||
| 76 | /*  | 
            ||
| 77 | * Function: init  | 
            ||
| 78 | *  | 
            ||
| 79 | * function to initialise the csrfProtector work flow  | 
            ||
| 80 | *  | 
            ||
| 81 | * Parameters:  | 
            ||
| 82 | * $length - length of CSRF_AUTH_TOKEN to be generated  | 
            ||
| 83 | * $action - int array, for different actions to be taken in case of failed validation  | 
            ||
| 84 | *  | 
            ||
| 85 | * Returns:  | 
            ||
| 86 | * void  | 
            ||
| 87 | *  | 
            ||
| 88 | * Throws:  | 
            ||
| 89 | * configFileNotFoundException - when configuration file is not found  | 
            ||
| 90 | * incompleteConfigurationException - when all required fields in config  | 
            ||
| 91 | * file are not available  | 
            ||
| 92 | *  | 
            ||
| 93 | */  | 
            ||
| 94 | public static function init($length = null, $action = null)  | 
            ||
| 166 | |||
| 167 | /*  | 
            ||
| 168 | * Function: authorizePost  | 
            ||
| 169 | * function to authorise incoming post requests  | 
            ||
| 170 | *  | 
            ||
| 171 | * Parameters:  | 
            ||
| 172 | * void  | 
            ||
| 173 | *  | 
            ||
| 174 | * Returns:  | 
            ||
| 175 | * void  | 
            ||
| 176 | *  | 
            ||
| 177 | * Throws:  | 
            ||
| 178 | * logDirectoryNotFoundException - if log directory is not found  | 
            ||
| 179 | */  | 
            ||
| 180 | public static function authorizePost()  | 
            ||
| 216 | |||
| 217 | /*  | 
            ||
| 218 | * Function: isValidToken  | 
            ||
| 219 | * function to check the validity of token in session array  | 
            ||
| 220 | * Function also clears all tokens older than latest one  | 
            ||
| 221 | *  | 
            ||
| 222 | * Parameters:  | 
            ||
| 223 | * $token - the token sent with GET or POST payload  | 
            ||
| 224 | *  | 
            ||
| 225 | * Returns:  | 
            ||
| 226 | * bool - true if its valid else false  | 
            ||
| 227 | */  | 
            ||
| 228 |         private static function isValidToken($token) { | 
            ||
| 251 | |||
| 252 | /*  | 
            ||
| 253 | * Function: failedValidationAction  | 
            ||
| 254 | * function to be called in case of failed validation  | 
            ||
| 255 | * performs logging and take appropriate action  | 
            ||
| 256 | *  | 
            ||
| 257 | * Parameters:  | 
            ||
| 258 | * void  | 
            ||
| 259 | *  | 
            ||
| 260 | * Returns:  | 
            ||
| 261 | * void  | 
            ||
| 262 | */  | 
            ||
| 263 | private static function failedValidationAction()  | 
            ||
| 310 | |||
| 311 | /*  | 
            ||
| 312 | * Function: refreshToken  | 
            ||
| 313 | * Function to set auth cookie  | 
            ||
| 314 | *  | 
            ||
| 315 | * Parameters:  | 
            ||
| 316 | * void  | 
            ||
| 317 | *  | 
            ||
| 318 | * Returns:  | 
            ||
| 319 | * void  | 
            ||
| 320 | */  | 
            ||
| 321 | public static function refreshToken()  | 
            ||
| 338 | |||
| 339 | /*  | 
            ||
| 340 | * Function: generateAuthToken  | 
            ||
| 341 | * function to generate random hash of length as given in parameter  | 
            ||
| 342 | * max length = 128  | 
            ||
| 343 | *  | 
            ||
| 344 | * Parameters:  | 
            ||
| 345 | * length to hash required, int  | 
            ||
| 346 | *  | 
            ||
| 347 | * Returns:  | 
            ||
| 348 | * string, token  | 
            ||
| 349 | */  | 
            ||
| 350 | public static function generateAuthToken()  | 
            ||
| 375 | |||
| 376 | /*  | 
            ||
| 377 | * Function: ob_handler  | 
            ||
| 378 | * Rewrites <form> on the fly to add CSRF tokens to them. This can also  | 
            ||
| 379 | * inject our JavaScript library.  | 
            ||
| 380 | *  | 
            ||
| 381 | * Parameters:  | 
            ||
| 382 | * $buffer - output buffer to which all output are stored  | 
            ||
| 383 | * $flag - INT  | 
            ||
| 384 | *  | 
            ||
| 385 | * Return:  | 
            ||
| 386 | * string, complete output buffer  | 
            ||
| 387 | */  | 
            ||
| 388 | public static function ob_handler($buffer, $flags)  | 
            ||
| 433 | |||
| 434 | /*  | 
            ||
| 435 | * Function: logCSRFattack  | 
            ||
| 436 | * Functio to log CSRF Attack  | 
            ||
| 437 | *  | 
            ||
| 438 | * Parameters:  | 
            ||
| 439 | * void  | 
            ||
| 440 | *  | 
            ||
| 441 | * Retruns:  | 
            ||
| 442 | * void  | 
            ||
| 443 | *  | 
            ||
| 444 | * Throws:  | 
            ||
| 445 | * logFileWriteError - if unable to log an attack  | 
            ||
| 446 | */  | 
            ||
| 447 | private static function logCSRFattack()  | 
            ||
| 482 | |||
| 483 | /*  | 
            ||
| 484 | * Function: getCurrentUrl  | 
            ||
| 485 | * Function to return current url of executing page  | 
            ||
| 486 | *  | 
            ||
| 487 | * Parameters:  | 
            ||
| 488 | * void  | 
            ||
| 489 | *  | 
            ||
| 490 | * Returns:  | 
            ||
| 491 | * string - current url  | 
            ||
| 492 | */  | 
            ||
| 493 | private static function getCurrentUrl()  | 
            ||
| 509 | |||
| 510 | /*  | 
            ||
| 511 | * Function: isURLallowed  | 
            ||
| 512 | * Function to check if a url mataches for any urls  | 
            ||
| 513 | * Listed in config file  | 
            ||
| 514 | *  | 
            ||
| 515 | * Parameters:  | 
            ||
| 516 | * void  | 
            ||
| 517 | *  | 
            ||
| 518 | * Returns:  | 
            ||
| 519 | * boolean - true is url need no validation, false if validation needed  | 
            ||
| 520 | */  | 
            ||
| 521 |         public static function isURLallowed() { | 
            ||
| 530 | };  | 
            ||
| 531 | }  | 
            ||
| 532 | 
This check marks private properties in classes that are never used. Those properties can be removed.