PHP Code Review - CodeWorks 2009 Edition

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites & 1 Group

    PHP Code Review - CodeWorks 2009 Edition - Presentation Transcript

    1. PHP Code Review Sebastian Bergmann | Arne Blankerts | Stefan Priebsch Copyright © 2009 thePHP.cc, Germany
    2. Who we are Premium PHP Consulting & Training. Worldwide. Sebastian Arne Stefan Bergmann Blankerts Priebsch
    3. Code Review ”The best way to prepare [to be a programmer] is to write programs, and to study great programs that other people have written. In my case, I went to the garbage cans at the Computer Science Center and fished out listings of their operating system.” - Bill Gates
    4. Code Review Automatic Analysis and Manual Review to improve code quality.
    5. Code Review  Internal  Over-the-Shoulder  Email Pass-Around  Pair Programming  Tool-Assisted Code Review  External
    6. Management vs. Code Reviews ”Stop those code reviews! They're slowing down the project.” This slide contains material by Johanna Rothmann
    7. Management vs. Code Reviews ”But then we won't know where the bugs are. We need the code reviews.” This slide contains material by Johanna Rothmann
    8. Management vs. Code Reviews ”Stop them or I'll fire you.” This slide contains material by Johanna Rothmann
    9. Management vs. Code Reviews ”You'll fire me for doing the right thing?” This slide contains material by Johanna Rothmann
    10. Management vs. Code Reviews ”In this case, the right thing is to finish the project as fast as possible. Stop those code reviews.” This slide contains material by Johanna Rothmann
    11. You may be wondering ... … what we will be doing here today in this workshop.
    12. Motivation and Disclaimer  In our daily work as consultants we see quite a few cases of bad code.  Naturally, we cannot talk about our customers' code.  Instead, we use examples from Open Source PHP projects.  We might come across harsh.  But it is not our intention to bash any of the Open Source projects we will look at.
    13. CakePHP Release 1.2.5 sb@ubuntu src % phploc --count-tests --exclude cake-1.2.5/vendors cake-1.2.5 phploc 1.3.0 by Sebastian Bergmann. Directories: 62 Files: 420 Lines of Code (LOC): 162937 Cyclomatic Complexity / Lines of Code: 0.11 Executable Lines of Code (ELOC): 85582 Comment Lines of Code (CLOC): 52689 Non-Comment Lines of Code (NCLOC): 110248 Interfaces: 0 Classes: 614 Abstract Classes: 0 Concrete Classes: 614 Lines of Code / Number of Classes: 265 Methods: 2322 Non-Static Methods: 2322 Static Methods: 0 Lines of Code / Number of Methods: 70 Cyclomatic Complexity / Number of Methods: 4.70 Functions: 62 Constants: 182 Global constants: 182 Class constants: 0 Tests: Classes: 92 Methods: 1183
    14. CakePHP index.php (Release 1.2.5) 044 if (function_exists('ini_set')) { 045 ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS); 046 define('APP_PATH', null); 047 define('CORE_PATH', null); 048 } else { 049 define('APP_PATH', ROOT . DS . APP_DIR . DS); 050 define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS); 051 }
    15. CakePHP cake/lib/class_registry.php (Release 1.2.5) 003 /** 004 * Class collections. 005 * 006 * A repository for class objects, each registered with a key.
    16. CakePHP cake/lib/class_registry.php (Release 1.2.5) 037 class ClassRegistry { 072 /** 073 * Loads a class, registers the object in the registry and returns instance of the object. 097 */ 352 }
    17. CakePHP cake/lib/class_registry.php (Release 1.2.5) 037 class ClassRegistry { 181 function addObject($key, &$object) { 182 $_this =& ClassRegistry::getInstance(); 183 $key = Inflector::underscore($key); 184 if (!isset($_this->__objects[$key])) { 185 $_this->__objects[$key] =& $object; 186 return true; 187 } 188 return false; 189 } 352 }
    18. CakePHP cake/lib/class_registry.php (Release 1.2.5) 041 class Controller extends Object { 636 /** 637 * Convenience method for header() 638 * 639 * @param string $status 640 * @return void 641 * @access public 642 */ 643 function header($status) { 644 header($status); 645 } 1177 }
    19. CakePHP cake/lib/object.php (Release 1.2.5) 037 class Object { 207 /** 208 * Checks for a persistent class file, if found file is opened and true returned 209 * If file is not found a file is created and false returned 210 * If used in other locations of the model you should choose a unique name for the persistent file 211 * There are many uses for this method, see manual for examples 212 * 213 * @param string $name name of the class to persist 214 * @param string $object the object to persist 215 * @return boolean Success 216 * @access protected 217 * @todo add examples to manual 218 */ 219 function _persist($name, $return = null, &$object, $type = null) { 236 } 297 }
    20. CakePHP cake/lib/object.php (Release 1.2.5) 037 class Object { 207 /** 208 * Checks for a persistent class file, if found file is opened and true returned 209 * If file is not found a file is created and false returned 210 * If used in other locations of the model you should choose a unique name for the persistent file 211 * There are many uses for this method, see manual for examples 212 * 213 * @param string $name name of the class to persist 214 * @param string $object the object to persist 215 * @return boolean Success 216 * @access protected 217 * @todo add examples to manual 218 */ 219 function _persist($name, $return = null, &$object, $type = null) { 236 } 297 }
    21. CakePHP cake/lib/object.php (Release 1.2.5) 037 class Object { 219 function _persist($name, $return = null, &$object, $type = null) { 220 $file = CACHE . 'persistent' . DS . strtolower($name) . '.php'; 221 if ($return === null) { 222 if (!file_exists($file)) { 223 return false; 224 } else { 225 return true; 226 } 227 } 228 229 if (!file_exists($file)) { 230 $this->_savePersistent($name, $object); 231 return false; 232 } else { 233 $this->__openPersistent($name, $type); 234 return true; 235 } 236 } 297 }
    22. CakePHP cake/lib/session.php (Release 1.2.5) 34 if (!class_exists('Set')) { 35 require LIBS . 'set.php'; 36 } 37 if (!class_exists('Security')) { 38 require LIBS . 'security.php'; 39 }
    23. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 188 /** 189 * Returns true if given variable is set in session. 190 * 191 * @param string $name Variable name to check for 192 * @return boolean True if variable is there 193 * @access public 194 */ 195 function check($name) { 196 $var = $this->__validateKeys($name); 197 if (empty($var)) { 198 return false; 199 } 200 $result = Set::extract($_SESSION, $var); 201 return isset($result); 202 } 778 }
    24. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 627 /** 628 * Validate that the $name is in correct dot notation 629 * example: $name = 'ControllerName.key'; 630 * 631 * @param string $name Session key names as string. 632 * @return mixed false is $name is not correct format, or $name if it is correct 633 * @access private 634 */ 635 function __validateKeys($name) { 636 if (is_string($name) && preg_match("/^[ 0-9a-zA-Z._-]*$/", $name)) { 637 return $name; 638 } 639 $this->__setError(3, "$name is not a string"); 640 return false; 641 } 778 }
    25. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 188 /** 189 * Returns true if given variable is set in session. 190 * 191 * @param string $name Variable name to check for 192 * @return boolean True if variable is there 193 * @access public 194 */ 195 function check($name) { 196 $var = $this->__validateKeys($name); 197 if (empty($var)) { 198 return false; 199 } 200 $result = Set::extract($_SESSION, $var); 201 return isset($result); 202 } 778 }
    26. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 203 /** 204 * Returns the Session id 205 * 206 * @param id $name string 207 * @return string Session id 208 * @access public 209 */ 210 function id($id = null) { 211 if ($id) { 212 $this->id = $id; 213 session_id($this->id); 214 } 215 if (isset($_SESSION)) { 216 return session_id(); 217 } else { 218 return $this->id; 219 } 220 } 778 }
    27. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 221 /** 222 * Removes a variable from session. 223 * 224 * @param string $name Session variable to remove 225 * @return boolean Success 226 * @access public 227 */ 228 function del($name) { 229 if ($this->check($name)) { 230 if ($var = $this->__validateKeys($name)) { 231 if (in_array($var, $this->watchKeys)) { 232 trigger_error('Deleting session key {' . $var . '}', E_USER_NOTICE); 233 } 234 $this->__overwrite($_SESSION, Set::remove($_SESSION, $var)); 235 return ($this->check($var) == false); 236 } 237 } 238 $this->__setError(2, "$name doesn't exist"); 239 return false; 240 } 778 }
    28. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 228 function del($name) { 229 if ($this->check($name)) { 230 if ($var = $this->__validateKeys($name)) { 231 if (in_array($var, $this->watchKeys)) { 232 trigger_error('Deleting session key {' . $var . '}', E_USER_NOTICE); 233 } 234 $this->__overwrite($_SESSION, Set::remove($_SESSION, $var)); 235 return ($this->check($var) == false); 236 } 237 } 238 $this->__setError(2, "$name doesn't exist"); 239 return false; 240 } 778 }
    29. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 548 /** 549 * Helper method to create a new session. 550 * 551 * @return void 552 * @access protected 553 */ 554 function _checkValid() { 583 } 778 }
    30. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 548 /** 549 * Helper method to create a new session. 550 * 551 * @return void 552 * @access protected 553 */ 554 function _checkValid() { 583 } 778 }
    31. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 554 function _checkValid() { 555 if ($this->read('Config')) { 576 } else { 577 $this->write('Config.userAgent', $this->_userAgent); 578 $this->write('Config.time', $this->sessionTime); 579 $this->write('Config.timeout', 10); 580 $this->valid = true; 581 $this->__setError(1, 'Session is valid'); 582 } 583 } 778 }
    32. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 554 function _checkValid() { 555 if ($this->read('Config')) { 576 } else { 577 $this->write('Config.userAgent', $this->_userAgent); 578 $this->write('Config.time', $this->sessionTime); 579 $this->write('Config.timeout', 10); 580 $this->valid = true; 581 $this->__setError(1, 'Session is valid'); 582 } 583 } 778 }
    33. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 287 /** 288 * Returns true if session is valid. 289 * 290 * @return boolean Success 291 * @access public 292 */ 293 function valid() { 294 if ($this->read('Config')) { 295 if ((Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read('Config.userAgent')) && $this->time <= $this->read('Config.time')) { 296 if ($this->error === false) { 297 $this->valid = true; 298 } 299 } else { 300 $this->valid = false; 301 $this->__setError(1, 'Session Highjacking Attempted !!!'); 302 } 303 } 304 return $this->valid; 305 } 778 }
    34. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 642 /** 643 * Helper method to set an internal error message. 644 * 645 * @param integer $errorNumber Number of the error 646 * @param string $errorMessage Description of the error 647 * @return void 648 * @access private 649 */ 650 function __setError($errorNumber, $errorMessage) { 651 if ($this->error === false) { 652 $this->error = array(); 653 } 654 $this->error[$errorNumber] = $errorMessage; 655 $this->lastError = $errorNumber; 656 } 778 }
    35. CakePHP cake/lib/session.php (Release 1.2.5) 049 class CakeSession extends Object { 752 /** 753 * Method called on the destruction of a database session. 754 * 755 * @param integer $key Key that uniquely identifies session in database 756 * @return boolean Success 757 * @access private 758 */ 759 function __destroy($key) { 760 $db =& ConnectionManager::getDataSource( Configure::read('Session.database') ); 761 $table = $db->fullTableName(Configure::read('Session.table')); 762 $db->execute("DELETE FROM " . $db->name($table) . " WHERE " . $db->name($table.'.id') . " = " . $db->value($key)); 763 return true; 764 } 778 }
    36. CakePHP cake/lib/security.php (Release 1.2.5) 035 class Security extends Object { 092 /** 093 * Validate authorization hash. 094 * 095 * @param string $authKey Authorization hash 096 * @return boolean Success 097 * @access public 098 * @static 099 * @todo Complete implementation 100 */ 101 function validateAuthKey($authKey) { 102 return true; 103 } 195 }
    37. CakePHP cake/lib/security.php (Release 1.2.5) 035 class Security extends Object { 043 /** 044 * Singleton implementation to get object instance. 045 * 046 * @return object 047 * @access public 048 * @static 049 */ 050 function &getInstance() { 051 static $instance = array(); 052 if (!$instance) { 053 $instance[0] =& new Security; 054 } 055 return $instance[0]; 056 } 101 function validateAuthKey($authKey) { 102 return true; 103 } 195 }
    38. CakePHP cake/lib/security.php (Release 1.2.5) 035 class Security extends Object { 172 function cipher($text, $key) { 173 if (empty($key)) { 174 trigger_error(__('You cannot use an empty key for Security::cipher()', true), E_USER_WARNING); 175 return ''; 176 } 194 } 195 }
    39. CakePHP cake/lib/file.php (Release 1.2.5) 041 class File extends Object { 094 function __construct($path, $create = false, $mode = 0755) { 102 if (!$this->exists()) { 103 if ($create === true) { 104 if ($this->safe($path) && $this->create() === false) { 105 return false; 106 } 107 } else { 108 return false; 109 } 110 } 111 } 507 }
    40. Drupal Release 6.13 sb@ubuntu src % phploc --count-tests --suffixes inc,php drupal-6.13 phploc 1.3.0 by Sebastian Bergmann. Directories: 34 Files: 142 Lines of Code (LOC): 44324 Cyclomatic Complexity / Lines of Code: 0.18 Executable Lines of Code (ELOC): 23808 Comment Lines of Code (CLOC): 17824 Non-Comment Lines of Code (NCLOC): 26500 Interfaces: 0 Classes: 1 Abstract Classes: 0 Concrete Classes: 1 Lines of Code / Number of Classes: 44324 Methods: 4 Non-Static Methods: 4 Static Methods: 0 Lines of Code / Number of Methods: 11081 Cyclomatic Complexity / Number of Methods: 4.50 Functions: 1204 Constants: 94 Global constants: 94 Class constants: 0 Tests: Classes: 0 Methods: 0
    41. Drupal includes/bootstrap.inc (Release 6.13) 152 function timer_start($name) { 153 global $timers; 154 155 list($usec, $sec) = explode(' ', microtime()); 156 $timers[$name]['start'] = (float)$usec + (float)$sec; 157 $timers[$name]['count'] = isset($timers[$name]['count']) ? ++$timers[$name]['count'] : 1; 158 }
    42. Drupal includes/bootstrap.inc (Release 6.13) 297 function conf_init() { 298 global $base_url, $base_path, $base_root; 299 300 // Export the following settings.php variables to the global namespace 301 global $db_url, $db_prefix, $cookie_domain, $conf, $installed_profile, $update_free_access; 302 $conf = array(); 303 304 if (isset($_SERVER['HTTP_HOST'])) { 305 // As HTTP_HOST is user input, ensure it only contains characters allowed 306 // in hostnames. See RFC 952 (and RFC 2181). 307 // $_SERVER['HTTP_HOST'] is lowercased here per specifications. 308 $_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']); 309 if (!drupal_valid_http_host($_SERVER['HTTP_HOST'])) { 310 // HTTP_HOST is invalid, e.g. if containing slashes it may be an attack. 311 header('HTTP/1.1 400 Bad Request'); 312 exit; 313 } 314 } 315 else { 316 // Some pre-HTTP/1.1 clients will not send a Host header. Ensure the key is 317 // defined for E_ALL compliance. 318 $_SERVER['HTTP_HOST'] = ''; 319 } 393 }
    43. Drupal includes/bootstrap.inc (Release 6.13) 321 if (file_exists('./'. conf_path() .'/settings.php')) { 322 include_once './'. conf_path() .'/settings.php'; 323 } 242 function conf_path($require_settings = TRUE, $reset = FALSE) { 255 if (file_exists("$confdir/$dir/settings.php") || (!$require_settings && file_exists("$confdir/$dir"))) { 256 $conf = "$confdir/$dir"; 257 return $conf; 258 } 263 }
    44. Drupal includes/bootstrap.inc (Release 6.13) 363 // Otherwise use $base_url as session name, without the protocol 364 // to use the same session identifiers across http and https. 371 // To prevent session cookies from being hijacked, a user can configure the 372 // SSL version of their website to only transfer session cookies via SSL by 373 // using PHP's session.cookie_secure setting. The browser will then use two 374 // separate session cookies for the HTTPS and HTTP versions of the site. So we 375 // must use different session identifiers for HTTPS and HTTP to prevent a 376 // cookie collision.
    45. Drupal includes/bootstrap.inc (Release 6.13) 363 // Otherwise use $base_url as session name, without the protocol 364 // to use the same session identifiers across http and https. 371 // To prevent session cookies from being hijacked, a user can configure the 372 // SSL version of their website to only transfer session cookies via SSL by 373 // using PHP's session.cookie_secure setting. The browser will then use two 374 // separate session cookies for the HTTPS and HTTP versions of the site. So we 375 // must use different session identifiers for HTTPS and HTTP to prevent a 376 // cookie collision.
    46. Drupal includes/bootstrap.inc (Release 6.13) 423 function drupal_get_filename($type, $name, $filename = NULL) { 424 static $files = array(); 425 426 if (!isset($files[$type])) { 427 $files[$type] = array(); 428 } 429 430 if (!empty($filename) && file_exists($filename)) { 431 $files[$type][$name] = $filename; 432 } 433 elseif (isset($files[$type][$name])) { 434 // nothing 435 } 436 // Verify that we have an active database connection, before querying 437 // the database. This is required because this function is called both 438 // before we have a database connection (i.e. during installation) and 439 // when a database connection fails. 440 elseif (db_is_active() && (($file = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file))) { 441 $files[$type][$name] = $file; 442 } 443 else { 444 // Fallback to searching the filesystem if the database connection is 445 // not established or the requested file is not found. 446 $config = conf_path(); 447 $dir = (($type == 'theme_engine') ? 'themes/engines' : "${type}s"); 448 $file = (($type == 'theme_engine') ? "$name.engine" : "$name.$type"); 449 450 foreach (array("$config/$dir/$file", "$config/$dir/$name/$file", "$dir/$file", "$dir/$name/$file") as $file) { 451 if (file_exists($file)) { 452 $files[$type][$name] = $file; 453 break; 454 } 455 } 456 } 457 458 if (isset($files[$type][$name])) { 459 return $files[$type][$name]; 460 } 461 }
    47. Elgg Revision 3264 sb@ubuntu src % phploc --count-tests elgg-r3264 phploc 1.3.0 by Sebastian Bergmann. Directories: 226 Files: 578 Lines of Code (LOC): 55269 Cyclomatic Complexity / Lines of Code: 0.20 Executable Lines of Code (ELOC): 22086 Comment Lines of Code (CLOC): 19054 Non-Comment Lines of Code (NCLOC): 36215 Interfaces: 6 Classes: 83 Abstract Classes: 11 Concrete Classes: 72 Lines of Code / Number of Classes: 665 Methods: 538 Non-Static Methods: 531 Static Methods: 7 Lines of Code / Number of Methods: 102 Cyclomatic Complexity / Number of Methods: 1.91 Functions: 693 Constants: 22 Global constants: 22 Class constants: 0 Tests: Classes: 0 Methods: 0
    48. Elgg entities.php (revision 3264) 31 abstract class ElggEntity implements 32 Notable, // Calendar interface 33 Locatable, // Geocoding interface 34 Exportable, // Allow export of data 35 Importable, // Allow import of data 36 Loggable, // Can events related to this object class be logged 37 Iterator, // Override foreach behaviour 38 ArrayAccess // Override for array access 39 { 1033 } 1034 2801 // functions in the global namespace
    49. Elgg entities.php (revision 3264) 195 /** 196 * Class member get overloading 197 * 198 * @param string $name 199 * @return mixed 200 */ 201 function __get($name) { return $this->get($name); }
    50. Elgg entities.php (revision 3264) 125 public function get($name) 126 { 127 // See if its in our base attribute 128 if (isset($this->attributes[$name])) { 129 return $this->attributes[$name]; 130 } 131 132 // No, so see if its in the meta data for this entity 133 $meta = $this->getMetaData($name); 134 if ($meta) 135 return $meta; 136 137 // Can't find it, so return null 138 return null; 139 }
    51. Elgg users.php (revision 3264) 29 class ElggUser extends ElggEntity 30 implements Friendable 31 { 168 public function ban($reason = "") { return ban_user($this->guid, $reason); } 338 } 339 1565 // functions in the global namespace
    52. Elgg users.php (revision 3264) 1550 function users_settings_save() { 1552 global $CONFIG; 1553 @include($CONFIG->path . "actions/user/name.php"); 1554 @include($CONFIG->path . "actions/user/password.php"); 1555 @include($CONFIG->path . "actions/email/save.php"); 1556 @include($CONFIG->path . "actions/user/language.php"); 1557 @include($CONFIG->path . "actions/user/default_access.php"); 1559 }
    53. Habari Revision 3563 sb@ubuntu src % phploc --count-tests habari-r3563 phploc 1.3.0 by Sebastian Bergmann. Directories: 25 Files: 255 Lines of Code (LOC): 43975 Cyclomatic Complexity / Lines of Code: 0.18 Executable Lines of Code (ELOC): 21898 Comment Lines of Code (CLOC): 12396 Non-Comment Lines of Code (NCLOC): 31579 Interfaces: 5 Classes: 138 Abstract Classes: 6 Concrete Classes: 132 Lines of Code / Number of Classes: 318 Methods: 1458 Non-Static Methods: 948 Static Methods: 510 Lines of Code / Number of Methods: 30 Cyclomatic Complexity / Number of Methods: 3.35 Functions: 7 Constants: 88 Global constants: 33 Class constants: 55 Tests: Classes: 14 Methods: 128
    54. Habari index.php (revision 3563) 192 // If we're doing unit testing, stop here 193 if ( defined( 'UNIT_TEST' ) ) { 194 return; 195 }
    55. Habari index.php (revision 3563) 52 // Replace all of the $_GET, $_POST and $_SERVER superglobals with object 53 // representations of each. Unset $_REQUEST, which is evil. 54 // $_COOKIE must be set after sessions start 55 SuperGlobal::process_gps();
    56. Habari superglobal.php (revision 3563) 11 class SuperGlobal extends ArrayIterator 12 { 29 public static function process_gps() 30 { 50 } 263 }
    57. Habari superglobal.php (revision 3563) 11 class SuperGlobal extends ArrayIterator 12 { 29 public static function process_gps() 30 { 31 /* We should only revert the magic quotes once per page hit */ 32 static $revert = true; 33 34 if (!$revert) { 35 // our work has already been done 36 return; 37 } 38 39 if ( get_magic_quotes_gpc() ) { 40 $_GET = Utils::stripslashes($_GET); 41 $_POST = Utils::stripslashes($_POST); 42 } 43 44 $_GET = new SuperGlobal($_GET); 45 $_POST = new SuperGlobal($_POST); 46 $_SERVER = new SuperGlobal($_SERVER); 47 unset($_REQUEST); 48 49 $revert = false; 50 } 263 }
    58. Habari superglobal.php (revision 3563) 11 class SuperGlobal extends ArrayIterator 12 { 29 public static function process_gps() 30 { 44 $_GET = new SuperGlobal($_GET); 45 $_POST = new SuperGlobal($_POST); 46 $_SERVER = new SuperGlobal($_SERVER); 47 unset($_REQUEST); 50 } 263 }
    59. Habari superglobal.php (revision 3563) 11 class SuperGlobal extends ArrayIterator 12 { 29 public static function process_gps() 30 { 31 /* We should only revert the magic quotes once per page hit */ 32 static $revert = true; 33 34 if (!$revert) { 35 // our work has already been done 36 return; 37 } 38 49 $revert = false; 50 } 263 }
    60. Habari databaseconnection.php (revision 3563) 87 protected function load_tables() 88 { 92 else if ( isset( $_POST['table_prefix'] ) ) { 93 $prefix = $_POST['table_prefix']; 94 } 101 foreach ( $this->tables as $t ) { 102 $this->sql_tables[$t] = $prefix . $t; 103 $this->sql_tables_repl[$t] = '{' . $t . '}'; 104 } 105 }
    61. Habari databaseconnection.php (revision 3587) 87 protected function load_tables() 88 { 92 else if ( isset( $_POST['table_prefix'] ) && 93 (preg_replace('%[^a-zA-Z_]%', '', $_POST['table_prefix']) == 94 $_POST['table_prefix']) ) { 95 $prefix = $_POST['table_prefix']; 96 } 103 foreach ( $this->tables as $t ) { 104 $this->sql_tables[$t] = $prefix . $t; 105 $this->sql_tables_repl[$t] = '{' . $t . '}'; 106 } 107 }
    62. Habari databaseconnection.php (revision 3588) 87 protected function load_tables() 88 { 98 foreach ( $this->tables as $t ) { 99 $this->sql_tables[$t] = $prefix . $t; 100 $this->sql_tables_repl[$t] = '{' . $t . '}'; 101 } 102 }
    63. Habari session.php (revision 3563) 13 class Session 14 { 165 static function destroy( $session_id ) 166 { 167 $sql = 'DELETE FROM {sessions} WHERE token = ?'; 168 $args = array( $session_id ); 169 $sql = Plugins::filter( 'sessions_clean', $sql, 'destroy', $args ); 170 DB::query( $sql, $args ); 171 return true; 172 } 442 }
    64. Habari session.php (revision 3563) 13 class Session 14 { 350 static function remove_error( $key ) 351 { 352 unset( $_SESSION['errors'][$key] ); 353 return ( !isset( $_SESSION['errors'][$key] ) ? true : false ); 354 } 442 }
    65. Habari singleton.php (revision 3563) 13 abstract class Singleton 14 { 24 protected static function instance() 25 { 26 /* 27 * It is important to note that subclasses MUST override this 28 * method, as get_class will ALWAYS return 'Singleton' when 29 * subclasses call this method through inheritance 30 * return self::getInstanceOf( get_class() ); 31 */ 32 trigger_error(_t('Not implemented: instance'), E_USER_WARNING); 33 return null; 34 } 64 }
    66. Joomla Release 1.5.14 sb@ubuntu src % phploc --count-tests joomla-1.5.14 phploc 1.3.0 by Sebastian Bergmann. Directories: 398 Files: 1072 Lines of Code (LOC): 242367 Cyclomatic Complexity / Lines of Code: 0.17 Executable Lines of Code (ELOC): 121338 Comment Lines of Code (CLOC): 81712 Non-Comment Lines of Code (NCLOC): 160655 Interfaces: 0 Classes: 749 Abstract Classes: 0 Concrete Classes: 749 Lines of Code / Number of Classes: 323 Methods: 5066 Non-Static Methods: 5065 Static Methods: 1 Lines of Code / Number of Methods: 47 Cyclomatic Complexity / Number of Methods: 3.95 Functions: 452 Constants: 637 Global constants: 637 Class constants: 0 Tests: Classes: 0 Methods: 0
    67. Joomla includes/framework.php (Release 1.5.14) 024 /* 025 * Installation check, and check on removal of the install directory. 026 */ 027 if (!file_exists( JPATH_CONFIGURATION . DS . 'configuration.php' ) || (filesize( JPATH_CONFIGURATION . DS . 'configuration.php' ) < 10) || file_exists( JPATH_INSTALLATION . DS . 'index.php' )) { 028 if( file_exists( JPATH_INSTALLATION . DS . 'index.php' ) ) { 029 header( 'Location: installation/index.php' ); 030 exit(); 031 } else { 032 echo 'No configuration file found and no installation code available. Exiting...'; 033 exit(); 034 } 035 }
    68. Joomla includes/framework.php (Release 1.5.14) 067 require_once(JPATH_SITE.DS.'libraries'.DS.'joomla'.DS.'utilities'.DS. 'compat'.DS.'compat.php');
    69. Joomla libraries/joomla/factory.php (Release 1.5.14) 021 class JFactory 022 { 089 function &getSession($options = array()) 090 { 091 static $instance; 092 093 if (!is_object($instance)) { 094 $instance = JFactory::_createSession($options); 095 } 096 097 return $instance; 098 } 717 }
    70. Joomla libraries/joomla/factory.php (Release 1.5.14) 021 class JFactory 022 { 135 function &getDocument() 136 { 137 static $instance; 138 139 if (!is_object( $instance )) { 140 $instance = JFactory::_createDocument(); 141 } 142 143 return $instance; 144 } 717 }
    71. Joomla libraries/joomla/application/helper.php 026 class JApplicationHelper 027 { 038 function &getClientInfo($id = null, $byName = false) 039 { 045 $obj = new stdClass(); 046 047 // Site Client 048 $obj->id = 0; 049 $obj->name = 'site'; 050 $obj->path = JPATH_SITE; 051 $clients[0] = clone($obj); 052 053 // Administrator Client 054 $obj->id = 1; 055 $obj->name = 'administrator'; 056 $obj->path = JPATH_ADMINISTRATOR; 057 $clients[1] = clone($obj); 058 059 // Installation Client 060 $obj->id = 2; 061 $obj->name = 'installation'; 062 $obj->path = JPATH_INSTALLATION; 063 $clients[2] = clone($obj); 064 065 // XMLRPC Client 066 $obj->id = 3; 067 $obj->name = 'xmlrpc'; 068 $obj->path = JPATH_XMLRPC; 069 $clients[3] = clone($obj); 095 } 337 }
    72. Joomla libraries/joomla/application/helper.php 026 class JApplicationHelper 027 { 038 function &getClientInfo($id = null, $byName = false) 039 { 093 $null = null; 094 return $null; 095 } 337 }
    73. Magento Revision 34865 sb@ubuntu src % phploc --count-tests --exclude magento-r34865/lib/Zend magento-r34865 phploc 1.3.0 by Sebastian Bergmann. Directories: 1453 Files: 3446 Lines of Code (LOC): 434540 Cyclomatic Complexity / Lines of Code: 0.15 Executable Lines of Code (ELOC): 186932 Comment Lines of Code (CLOC): 164799 Non-Comment Lines of Code (NCLOC): 269741 Interfaces: 35 Classes: 3037 Abstract Classes: 90 Concrete Classes: 2947 Lines of Code / Number of Classes: 143 Methods: 16192 Non-Static Methods: 16026 Static Methods: 166 Lines of Code / Number of Methods: 26 Cyclomatic Complexity / Number of Methods: 2.74 Functions: 27 Constants: 1155 Global constants: 313 Class constants: 842 Tests: Classes: 0 Methods: 0
    74. Magento Action.php (revision 34865) 28 /** 29 * Custom Zend_Controller_Action class (formally) 36 */ 37 abstract class Mage_Core_Controller_Varien_Action 38 { 710 }
    75. Magento Action.php (revision 34865) 28 /** 29 * Custom Zend_Controller_Action class (formally) 36 */ 37 abstract class Mage_Core_Controller_Varien_Action 38 { 314 public function renderLayout($output='') 315 { 345 } 710 }
    76. Magento functions.php (revision 32041) 62 function __autoload($class) 63 { 64 if (strpos($class, '/')!==false) { 65 return; 66 } 67 $classFile = uc_words($class, DS).'.php'; 68 69 //$a = explode('_', $class); 70 //Varien_Profiler::start('AUTOLOAD'); 71 //Varien_Profiler::start('AUTOLOAD: '.$a[0]); 72 73 include($classFile); 74 75 //Varien_Profiler::stop('AUTOLOAD'); 76 //Varien_Profiler::stop('AUTOLOAD: '.$a[0]); 77 }
    77. Magento functions.php (revision 32041) 119 function uc_words($str, $destSep='_', $srcSep='_') 120 { 121 return str_replace( 122 ' ', 123 $destSep, 124 ucwords(str_replace($srcSep, ' ', $str)) 125 ); 126 }
    78. Magento Translate.php (revision 34865) 32 class Mage_Core_Model_Translate 33 { 374 public function translate($args) 375 { 403 $result = @vsprintf($translated, $args); 404 if ($result === false) { 405 $result = $translated; 406 } 419 } 568 }
    79. Magento Translate.php (revision 34865) 32 class Mage_Core_Model_Translate 33 { 374 public function translate($args) 375 { 403 $result = @vsprintf($translated, $args); 404 if ($result === false) { 405 $result = $translated; 406 } 407 408 if ($result === false){ 409 $result = $translated; 410 } 419 } 568 }
    80. Magento Translate.php (revision 34865) 32 class Mage_Core_Model_Translate 33 { 112 public function init($area, $forceReload = false) 113 { 119 if (!$forceReload && ($this->_data = $this->_loadCache())) { 120 if ($this->_canUseCache()) { 121 return $this; 122 } 123 Mage::app() ->removeCache($this->getCacheId()); 124 } 141 } 568 }
    81. Magento Translate.php (revision 34865) 32 class Mage_Core_Model_Translate 33 { 112 public function init($area, $forceReload = false) 113 { 119 if (!$forceReload && ($this->_data = $this->_loadCache())) { 120 if ($this->_canUseCache()) { 121 return $this; 122 } 123 Mage::app() ->removeCache($this->getCacheId()); 124 } 141 } 568 }
    82. Magento Translate.php (revision 34865) 32 class Mage_Core_Model_Translate 33 { 112 public function init($area, $forceReload = false) 113 { 119 if (!$forceReload && ($this->_data = $this->_loadCache())) { 120 if ($this->_canUseCache()) { 121 return $this; 122 } 123 Mage::app() ->removeCache($this->getCacheId()); 124 } 141 } 542 protected function _canUseCache() 543 { 544 return Mage::app()->useCache('translate'); 545 } 568 }
    83. Magento Translate.php (revision 34865) 32 class Mage_Core_Model_Translate 33 { 512 protected function _loadCache() 513 { 514 if (!$this->_canUseCache()) { 515 return false; 516 } 517 $data = Mage::app() ->loadCache($this->getCacheId()); 518 $data = unserialize($data); 519 return $data; 520 } 542 protected function _canUseCache() 543 { 544 return Mage::app()->useCache('translate'); 545 } 568 }
    84. Magento Translate.php (revision 34865) 32 class Mage_Core_Model_Translate 33 { 512 protected function _loadCache() 513 { 514 if (!$this->_canUseCache()) { 515 return false; 516 } 517 $data = Mage::app() ->loadCache($this->getCacheId()); 518 $data = unserialize($data); 519 return $data; 520 } 568 }
    85. Magento Translate.php (revision 34865) 32 class Mage_Core_Model_Translate 33 { 528 protected function _saveCache() 529 { 530 if (!$this->_canUseCache()) { 531 return $this; 532 } 533 Mage::app() ->saveCache(serialize($this->getData()), $this->getCacheId(), array(self::CACHE_TAG), null); 534 return $this; 535 } 568 }
    86. Magento functions.php (revision 32041) 84 function destruct($object) 85 { 86 if (is_array($object)) { 87 foreach ($object as $obj) { 88 destruct($obj); 89 } 90 } elseif (is_object($object)) { 91 if (in_array('__destruct', 92 get_class_methods($object))) { 93 $object->__destruct(); 94 } 95 } 96 unset($object); 97 }
    87. Magento Abstract.php (revision 34865) 35 abstract class Mage_Core_Model_Abstract extends Varien_Object 36 { 92 /** 93 * Standard model initialization 94 * 95 * @param string $resourceModel 96 * @param string $idFieldName 97 * @return Mage_Core_Model_Abstract 98 */ 99 protected function _init($resourceModel) 100 { 101 $this->_setResourceModel($resourceModel); 102 } 378 }
    88. Magento Abstract.php (revision 34865) 35 abstract class Mage_Core_Model_Abstract extends Varien_Object 36 { 141 public function getIdFieldName() 142 { 143 if (!($fieldName = parent::getIdFieldName())) { 144 $fieldName = $this->_getResource() ->getIdFieldName(); 145 $this->setIdFieldName($fieldName); 146 } 147 return $fieldName; 148 } 378 }
    89. Magento App.php (revision 34865) 806 public function getTranslator() 807 { 808 if (!$this->_translator) { 809 $this->_translator = Mage::getSingleton('core/translate'); 810 } 811 return $this->_translator; 812 }
    90. Magento App.php (revision 34865) 1110 public function getResponse() 1111 { 1112 if (empty($this->_response)) { 1113 $this->_response = new Mage_Core_Controller_Response_Http(); 1114 $this->_response->headersSentThrowsException = Mage::$headersSentThrowsException; 1115 $this->_response->setHeader("Content-Type", "text/html; charset=UTF-8"); 1116 } 1117 return $this->_response; 1118 }
    91. Magento Config.php (revision 34865) 37 class Mage_Core_Model_Config extends Mage_Core_Model_Config_Base 38 { 64 public function getResourceModel() 65 { 66 if (is_null($this->_resourceModel)) { 67 $this->_resourceModel = Mage::getResourceModel('core/config'); 68 } 69 return $this->_resourceModel; 70 } 1015 }
    92. Magento Config.php (revision 34865) 103 /** 104 * Initialization of core configuration 105 * 106 * @return Mage_Core_Model_Config 107 */ 108 public function init($options=array()) 109 { 230 }
    93. Magento Config.php (revision 34865) 103 /** 104 * Initialization of core configuration 105 * 106 * @return Mage_Core_Model_Config 107 */ 108 public function init($options=array()) 109 { 132 set_include_path( 133 // excluded '/app/code/local' 134 BP . DS . 'app' . DS . 'code' . DS . 'community' . PS . 135 BP . DS . 'app' . DS . 'code' . DS . 'core' . PS . 136 BP . DS . 'lib' . PS . 137 /** 138 * Problem with concatenate BP . $codeDir 139 */ 140 /*BP . $codeDir . DS .'community' . PS . 141 BP . $codeDir . DS .'core' . PS . 142 BP . $libDir . PS .*/ 143 Mage::registry('original_include_path') 144 ); 230 }
    94. Magento Collection.php (revision 34865) 86 $ioProxy = new Varien_Io_File(); 87 88 try { 89 $ioProxy->open(array('path'=>$readPath)); 90 } 91 catch (Exception $e) { 92 $ioProxy->mkdir($readPath, 0777); 93 $ioProxy->chmod($readPath, 0777); 94 $ioProxy->open(array('path'=>$readPath)); 95 }
    95. Magento functions.php (revision 32041) 104 function __() 105 { 106 return Mage::app() 107 ->getTranslator() 108 ->translate(func_get_args()); 109 }
    96. Magento items.phtml (revision 34865) 38 <?php $i=0; foreach ($_order->getAllItems() as $_item): ?> 39 <?php if($_item->getParentItem()) continue; else $i++; ?> 40 <tbody<?php echo $i%2 ? ' bgcolor="#eeeded"' : '' ?>> 41 <?php echo $this->getItemHtml($_item) ?> 42 </tbody> 43 <?php endforeach; ?>
    97. Shindig Revision 772122 sb@ubuntu src % phploc --count-tests --exclude shindig-r772122/php/external shindig-r772122/php phploc 1.3.0 by Sebastian Bergmann. Directories: 21 Files: 182 Lines of Code (LOC): 23266 Cyclomatic Complexity / Lines of Code: 0.14 Executable Lines of Code (ELOC): 10509 Comment Lines of Code (CLOC): 8692 Non-Comment Lines of Code (NCLOC): 14574 Interfaces: 8 Classes: 182 Abstract Classes: 16 Concrete Classes: 166 Lines of Code / Number of Classes: 127 Methods: 1298 Non-Static Methods: 1204 Static Methods: 94 Lines of Code / Number of Methods: 17 Cyclomatic Complexity / Number of Methods: 2.04 Functions: 4 Constants: 16 Global constants: 2 Class constants: 14 Tests: Classes: 43 Methods: 355
    98. Shindig index.php (revision 772122) 21 // Some people forget to set their timezone in their php.ini, 22 // this prevents that from generating warnings 23 @date_default_timezone_set(@date_default_timezone_get());
    99. Shindig index.php (revision 772122) 48 function __autoload($className) { 70 // Check for the presense of this class in our all our directories. 71 $fileName = $className . '.php'; 72 foreach ($locations as $path) { 73 if (file_exists("{$path}/$fileName")) { 74 require $path.'/'.$fileName; 75 break; 76 } 77 } 78 }
    100. Shindig index.php (revision 772122) 113 $class = new $class();
    101. Shindig index.php (revision 772122) 113 $class = new $class(); 114 $method = $_SERVER['REQUEST_METHOD']; 115 // Not all clients support the PUT, HEAD & DELETE http methods, they depend on the X-HTTP-Method-Override instead 116 if ($method == 'POST' && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { 117 $method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; 118 } 119 $method = 'do' . ucfirst(strtolower($method)); 120 if (is_callable(array($class, $method))) { 121 $class->$method(); 122 } else {
    102. TikiWiki Release 3.1 sb@ubuntu src % phploc --count-tests tikiwiki-3.1 phploc 1.3.0 by Sebastian Bergmann. Directories: 704 Files: 3603 Lines of Code (LOC): 1092274 Cyclomatic Complexity / Lines of Code: 0.15 Executable Lines of Code (ELOC): 478705 Comment Lines of Code (CLOC): 739588 Non-Comment Lines of Code (NCLOC): 352686 Interfaces: 58 Classes: 2547 Abstract Classes: 137 Concrete Classes: 2410 Lines of Code / Number of Classes: 428 Methods: 20075 Non-Static Methods: 19271 Static Methods: 804 Lines of Code / Number of Methods: 54 Cyclomatic Complexity / Number of Methods: 3.61 Functions: 1200 Constants: 4244 Global constants: 950 Class constants: 3294 Tests: Classes: 26 Methods: 202
    103. TikiWiki lib/banners/bannerlib.php (Release 3.1) 009 class BannerLib extends TikiLib { 100 function list_banners($offset = 0, $maxRecords = -1, $sort_mode = 'created_desc', $find = '', $user) { 135 } 271 }
    104. TikiWiki lib/banners/bannerlib.php (Release 3.1) 009 class BannerLib extends TikiLib { 100 function list_banners($offset = 0, $maxRecords = -1, $sort_mode = 'created_desc', $find = '', $user) { 121 $query = "select * from `tiki_banners` $mid order by ". $this->convert_sortmode($sort_mode); 122 $query_cant = "select count(*) from `tiki_banners` $mid"; 123 $result = $this->query($query,$bindvars,$maxRecords,$offset); 124 $cant = $this->getOne($query_cant,$bindvars); 125 $ret = array(); 126 127 while ($res = $result->fetchRow()) { 128 $ret[] = $res; 129 } 130 131 $retval = array(); 132 $retval["data"] = $ret; 133 $retval["cant"] = $cant; 134 return $retval; 135 } 271 }
    105. TikiWiki lib/banners/bannerlib.php (Release 3.1) 009 class BannerLib extends TikiLib { 094 function add_click($bannerId) { 095 $query = "update `tiki_banners` set `clicks` = `clicks` + 1 where `bannerId`=?"; 096 097 $result = $this->query($query,array((int)$bannerId)); 098 } 271 } banner_click.php 027 $bannerlib->add_click($_REQUEST["id"]);
    106. TikiWiki banner_click.php (Release 3.1) 027 $bannerlib->add_click($_REQUEST["id"]); 028 $url = urldecode($_REQUEST["url"]); 029 header ("location: $url");
    107. Wordpress Revision 10383 sb@ubuntu src % phploc --count-tests wordpress-r10383 phploc 1.3.0 by Sebastian Bergmann. Directories: 21 Files: 260 Lines of Code (LOC): 114387 Cyclomatic Complexity / Lines of Code: 0.23 Executable Lines of Code (ELOC): 57984 Comment Lines of Code (CLOC): 38680 Non-Comment Lines of Code (NCLOC): 75707 Interfaces: 0 Classes: 106 Abstract Classes: 0 Concrete Classes: 106 Lines of Code / Number of Classes: 1079 Methods: 1225 Non-Static Methods: 1225 Static Methods: 0 Lines of Code / Number of Methods: 93 Cyclomatic Complexity / Number of Methods: 4.73 Functions: 1658 Constants: 191 Global constants: 191 Class constants: 0 Tests: Classes: 0 Methods: 0
    108. Wordpress wp-db.php (revision 10383) 306 function __construct(...) { 307 register_shutdown_function( array(&$this, "__destruct") );
    109. Wordpress wp-db.php (revision 10383) 306 function __construct(...) { 307 register_shutdown_function( array(&$this, "__destruct") ); 358 function __destruct() { 359 return true; 360 }
    110. Wordpress wp-db.php (revision 10383) 428 function escape($string) { 429 return addslashes( $string ); 430 // Disable rest for now, causing problems 431 /* 432 if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' ) 433 return mysql_escape_string( $string ); 434 else 435 return mysql_real_escape_string( $string, $this->dbh ); 436 */ 437 }
    111. Wordpress wp-db.php (revision 10383) 428 function escape($string) { 429 return addslashes( $string ); 430 // Disable rest for now, causing problems 431 /* 432 if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' ) 433 return mysql_escape_string( $string ); 434 else 435 return mysql_real_escape_string( $string, $this->dbh ); 436 */ 437 } 446 function escape_by_ref(&$s) { 447 $s = $this->escape($s); 448 }

    + Sebastian BergmannSebastian Bergmann, 1 month ago

    custom

    952 views, 2 favs, 1 embeds more stats

    In this workshop, three PHP experts with different more

    More info about this document

    CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

    Go to text version

    • Total Views 952
      • 951 on SlideShare
      • 1 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 31
    Most viewed embeds
    • 1 views on http://beta.mixlog.it

    more

    All embeds
    • 1 views on http://beta.mixlog.it

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events