PHP Code Review

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

    6 Favorites, 1 Group & 1 Event

    PHP Code Review - Presentation Transcript

    1. PHP Code Review Sebastian Bergmann | Arne Blankerts | Stefan Priebsch May 19th 2009 Copyright © 2009 thePHP.cc, Germany
    2. Who we are Premium PHP Consulting & Training. Worldwide. Sebastian Arne Stefan Bergmann Blankerts Priebsch
    3. Elgg Revision 3264 sb@ubuntu src % phploc elgg phploc 1.1.0 by Sebastian Bergmann. Directories: 226 Files: 578 Lines of Code (LOC): 55269 Comment Lines of Code (CLOC): 19054 Non-Comment Lines of Code (NCLOC): 36215 Interfaces: 6 Classes: 83 Non-Static Methods: 521 Static Methods: 7 Functions: 703
    4. 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
    5. 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); }
    6. 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 }
    7. 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
    8. 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 }
    9. Habari Revision 3563 sb@ubuntu habari % phploc htdocs phploc 1.1.0 by Sebastian Bergmann. Directories: 20 Files: 228 Lines of Code (LOC): 41598 Comment Lines of Code (CLOC): 12122 Non-Comment Lines of Code (NCLOC): 29476 Interfaces: 5 Classes: 130 Non-Static Methods: 650 Static Methods: 406 Functions: 368
    10. Habari index.php (revision 3563) 192 // If we're doing unit testing, stop here 193 if ( defined( 'UNIT_TEST' ) ) { 194 return; 195 }
    11. 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();
    12. Habari superglobal.php (revision 3563) 11 class SuperGlobal extends ArrayIterator 12 { 29 public static function process_gps() 30 { 50 } 263 }
    13. 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 }
    14. 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 }
    15. 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 }
    16. 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 }
    17. 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 }
    18. 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 }
    19. 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 }
    20. Magento Revision 34865 sb@ubuntu src % phploc magento phploc 1.1.0 by Sebastian Bergmann. Directories: 1741 Files: 4839 Lines of Code (LOC): 715457 Comment Lines of Code (CLOC): 306283 Non-Comment Lines of Code (NCLOC): 409174 Interfaces: 88 Classes: 4378 Non-Static Methods: 23631 Static Methods: 733 Functions: 1199
    21. Magento Action.php (revision 34865) 28 /** 29 * Custom Zend_Controller_Action class (formally) 36 */ 37 abstract class Mage_Core_Controller_Varien_Action 38 { 710 }
    22. 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 }
    23. 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 }
    24. 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';
    25. 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';
    26. 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';
    27. Magento functions.php (revision 32041) 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 }
    28. 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 }
    29. 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 }
    30. 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 }
    31. 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 }
    32. 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 }
    33. 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 }
    34. 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 }
    35. 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 }
    36. 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 }
    37. 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 }
    38. 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 }
    39. 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 }
    40. 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 }
    41. 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 }
    42. 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 }
    43. 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 }
    44. 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 }
    45. 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 }
    46. 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 }
    47. 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 }
    48. Magento functions.php (revision 32041) 104 function __() 105 { 106 return Mage::app() 107 ->getTranslator() 108 ->translate(func_get_args()); 109 }
    49. 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; ?>
    50. Shindig Revision 772122 sb@ubuntu src % phploc shindig phploc 1.1.0 by Sebastian Bergmann. Directories: 80 Files: 525 Lines of Code (LOC): 81082 Comment Lines of Code (CLOC): 38968 Non-Comment Lines of Code (NCLOC): 42114 Interfaces: 42 Classes: 535 Non-Static Methods: 3128 Static Methods: 349 Functions: 125
    51. 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());
    52. 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 }
    53. Shindig index.php (revision 772122) 113 $class = new $class();
    54. 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 {
    55. SugarCRM CurrencyService.php (SugarCE 5.2.0a) 44 class CurrencyService { 68 function insertDefaults() { 69 global $sugar_config; 70 71 $insert=true; 72 73 if($insert) { 80 } 81 } 84 }
    56. SugarCRM DBManager.php (SugarCE 5.2.0a) 93 abstract class DBManager 94 { 179 public function getDatabase() 180 { 181 $this->checkConnection(); 182 return $this->database; 183 } 1729 }
    57. Wordpress wp-db.php (revision 10383) 306 function __construct(...) { 307 register_shutdown_function( array(&$this, \"__destruct\") );
    58. Wordpress wp-db.php (revision 10383) 306 function __construct(...) { 307 register_shutdown_function( array(&$this, \"__destruct\") ); 358 function __destruct() { 359 return true; 360 }
    59. 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 }
    60. 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, 6 months ago

    custom

    2762 views, 6 favs, 4 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 2762
      • 2645 on SlideShare
      • 117 from embeds
    • Comments 0
    • Favorites 6
    • Downloads 97
    Most viewed embeds
    • 109 views on http://sebastian-bergmann.de
    • 5 views on http://www.planet-php.net
    • 2 views on http://duniarusak.blogspot.com
    • 1 views on http://favit.dev

    more

    All embeds
    • 109 views on http://sebastian-bergmann.de
    • 5 views on http://www.planet-php.net
    • 2 views on http://duniarusak.blogspot.com
    • 1 views on http://favit.dev

    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