From c3400aa39cf4c0a55e993533be3e802d67f488d6 Mon Sep 17 00:00:00 2001 From: "Theodore R. Smith" Date: Wed, 15 May 2019 09:54:29 -0500 Subject: [PATCH 1/2] Added php-cs-fixer and fixed up most of the code to PSR-2 standards. --- .gitignore | 4 +++ .php_cs | 26 +++++++++++++++ ChromePHP.php | 90 ++++++++++++++++++++++++++++----------------------- composer.json | 3 ++ 4 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 .gitignore create mode 100644 .php_cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c1a481 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +composer.lock +.idea/ +.php_cs.cache +vendor/ \ No newline at end of file diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..bd7d760 --- /dev/null +++ b/.php_cs @@ -0,0 +1,26 @@ +setRules([ + '@Symfony' => true, + 'elseif' => false, + 'yoda_style' => false, + 'list_syntax' => ['syntax' => 'short'], + 'concat_space' => ['spacing' => 'one'], + 'binary_operator_spaces' => [ + 'align_equals' => false, + 'align_double_arrow' => true, + ], + 'declare_strict_types' => false, + 'no_superfluous_elseif' => true, + 'blank_line_after_opening_tag' => false + ]) + ->setFinder( + PhpCsFixer\Finder::create() + ->exclude('bootstrap') + ->exclude('config') + ->exclude('legacy') + ->exclude('packages') + ->exclude('vendor') + ->in(__DIR__) + ); diff --git a/ChromePHP.php b/ChromePHP.php index bcab5d7..c2dbca1 100755 --- a/ChromePHP.php +++ b/ChromePHP.php @@ -1,6 +1,6 @@ */ class ChromePHP @@ -99,7 +98,7 @@ class ChromePHP protected $_json = array( 'version' => self::VERSION, 'columns' => array('log', 'backtrace', 'type'), - 'rows' => array() + 'rows' => array(), ); /** @@ -116,7 +115,7 @@ class ChromePHP * @var array */ protected $_settings = array( - self::BACKTRACE_LEVEL => 1 + self::BACKTRACE_LEVEL => 1, ); /** @@ -125,14 +124,14 @@ class ChromePHP protected static $_instance; /** - * Prevent recursion when working with objects referring to each other + * Prevent recursion when working with objects referring to each other. * * @var array */ protected $_processed = array(); /** - * constructor + * constructor. */ private function __construct() { @@ -141,7 +140,7 @@ private function __construct() } /** - * gets instance of this class + * gets instance of this class. * * @return ChromePHP */ @@ -150,105 +149,111 @@ public static function getInstance() if (self::$_instance === null) { self::$_instance = new self(); } + return self::$_instance; } /** - * logs a variable to the console + * logs a variable to the console. * * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] - * @return void */ public static function log() { $args = func_get_args(); + return self::_log('', $args); } /** - * logs a warning to the console + * logs a warning to the console. * * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] - * @return void */ public static function warn() { $args = func_get_args(); + return self::_log(self::WARN, $args); } /** - * logs an error to the console + * logs an error to the console. * * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] - * @return void */ public static function error() { $args = func_get_args(); + return self::_log(self::ERROR, $args); } /** - * sends a group log + * sends a group log. * * @param string value */ public static function group() { $args = func_get_args(); + return self::_log(self::GROUP, $args); } /** - * sends an info log + * sends an info log. * * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] - * @return void */ public static function info() { $args = func_get_args(); + return self::_log(self::INFO, $args); } /** - * sends a collapsed group log + * sends a collapsed group log. * * @param string value */ public static function groupCollapsed() { $args = func_get_args(); + return self::_log(self::GROUP_COLLAPSED, $args); } /** - * ends a group log + * ends a group log. * * @param string value */ public static function groupEnd() { $args = func_get_args(); + return self::_log(self::GROUP_END, $args); } /** - * sends a table log + * sends a table log. * * @param string value */ public static function table() { $args = func_get_args(); + return self::_log(self::TABLE, $args); } /** - * internal logging call + * internal logging call. * * @param string $type + * * @return ChromePHP */ protected static function _log($type, array $args) @@ -277,9 +282,10 @@ protected static function _log($type, array $args) } /** - * converts an object to a better format for logging + * converts an object to a better format for logging. + * + * @param object * - * @param Object * @return array */ protected function _convert($object) @@ -301,7 +307,6 @@ protected function _convert($object) // loop through object vars $object_vars = get_object_vars($object); foreach ($object_vars as $key => $value) { - // same instance as parent object if ($value === $object || in_array($value, $this->_processed, true)) { $value = 'recursion - parent object [' . get_class($value) . ']'; @@ -313,7 +318,6 @@ protected function _convert($object) // loop through the properties and add those foreach ($reflection->getProperties() as $property) { - // if one of these properties was already added above then ignore it if (array_key_exists($property->getName(), $object_vars)) { continue; @@ -329,13 +333,15 @@ protected function _convert($object) $object_as_array[$type] = $this->_convert($value); } + return $object_as_array; } /** - * takes a reflection property and returns a nicely formatted key of the property name + * takes a reflection property and returns a nicely formatted key of the property name. * * @param ReflectionProperty $property + * * @return string */ protected function _getPropertyKey(ReflectionProperty $property) @@ -355,10 +361,9 @@ protected function _getPropertyKey(ReflectionProperty $property) } /** - * adds a value to the data array + * adds a value to the data array. * * @var mixed - * @return void */ protected function _addRow(array $logs, $backtrace, $type) { @@ -394,32 +399,35 @@ protected function _writeHeader($data) 'ChromePHP Error: The HTML header will surpass the limit of ' . $this->_formatSize(self::HTTPD_HEADER_LIMIT) . ' (' . $this->_formatSize(strlen($header)) . ') - You can increase the HTTPD_HEADER_LIMIT on ChromePHP class, according to your Apache ' . - 'LimitRequestFieldsize directive' - ], '', self::ERROR + 'LimitRequestFieldsize directive', + ], '', self::ERROR, ]; $header = self::HEADER_NAME . ': ' . $this->_encode($data); } header($header); } - protected function _formatSize($arg) { + protected function _formatSize($arg) + { if ($arg > 0) { $j = 0; - $ext = ["bytes","Kb","Mb","Gb","Tb"]; - while ($arg >= pow(1024, $j)) ++$j; { - $arg = (round($arg / pow(1024, $j - 1) * 100) / 100).($ext[$j - 1]); + $ext = ['bytes', 'Kb', 'Mb', 'Gb', 'Tb']; + while ($arg >= pow(1024, $j)) { + ++$j; } + $arg = (round($arg / pow(1024, $j - 1) * 100) / 100) . ($ext[$j - 1]); return $arg; } - return "0Kb"; + return '0Kb'; } /** - * encodes the data to be sent along with the request + * encodes the data to be sent along with the request. * * @param array $data + * * @return string */ protected function _encode($data) @@ -428,11 +436,10 @@ protected function _encode($data) } /** - * adds a setting + * adds a setting. * * @param string key * @param mixed value - * @return void */ public function addSetting($key, $value) { @@ -440,10 +447,9 @@ public function addSetting($key, $value) } /** - * add ability to set multiple settings in one call + * add ability to set multiple settings in one call. * * @param array $settings - * @return void */ public function addSettings(array $settings) { @@ -453,9 +459,10 @@ public function addSettings(array $settings) } /** - * gets a setting + * gets a setting. * * @param string key + * * @return mixed */ public function getSetting($key) @@ -463,6 +470,7 @@ public function getSetting($key) if (!isset($this->_settings[$key])) { return null; } + return $this->_settings[$key]; } } diff --git a/composer.json b/composer.json index 6d9fd20..627d8ef 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,8 @@ "psr-4": { "ChromePHP\\": "" } + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.15" } } From 7e4eef7c1303d93e9402c92d73768d5eb769cef0 Mon Sep 17 00:00:00 2001 From: "Theodore R. Smith" Date: Wed, 15 May 2019 09:54:52 -0500 Subject: [PATCH 2/2] Converted long arrays to short arrays. --- .php_cs | 1 + ChromePHP.php | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.php_cs b/.php_cs index bd7d760..1fb5891 100644 --- a/.php_cs +++ b/.php_cs @@ -6,6 +6,7 @@ return PhpCsFixer\Config::create() 'elseif' => false, 'yoda_style' => false, 'list_syntax' => ['syntax' => 'short'], + 'array_syntax' => ['syntax' => 'short'], 'concat_space' => ['spacing' => 'one'], 'binary_operator_spaces' => [ 'align_equals' => false, diff --git a/ChromePHP.php b/ChromePHP.php index c2dbca1..340482b 100755 --- a/ChromePHP.php +++ b/ChromePHP.php @@ -95,16 +95,16 @@ class ChromePHP /** * @var array */ - protected $_json = array( + protected $_json = [ 'version' => self::VERSION, - 'columns' => array('log', 'backtrace', 'type'), - 'rows' => array(), - ); + 'columns' => ['log', 'backtrace', 'type'], + 'rows' => [], + ]; /** * @var array */ - protected $_backtraces = array(); + protected $_backtraces = []; /** * @var bool @@ -114,9 +114,9 @@ class ChromePHP /** * @var array */ - protected $_settings = array( + protected $_settings = [ self::BACKTRACE_LEVEL => 1, - ); + ]; /** * @var ChromePHP @@ -128,7 +128,7 @@ class ChromePHP * * @var array */ - protected $_processed = array(); + protected $_processed = []; /** * constructor. @@ -265,8 +265,8 @@ protected static function _log($type, array $args) return $logger; } - $logger->_processed = array(); - $logs = array_map(array($logger, '_convert'), $args); + $logger->_processed = []; + $logs = array_map([$logger, '_convert'], $args); $backtrace = debug_backtrace(false); $level = $logger->getSetting(self::BACKTRACE_LEVEL); @@ -299,7 +299,7 @@ protected function _convert($object) //Also avoid recursion when objects refer to each other $this->_processed[] = $object; - $object_as_array = array(); + $object_as_array = []; // first add the class name $object_as_array['___class_name'] = get_class($object); @@ -382,7 +382,7 @@ protected function _addRow(array $logs, $backtrace, $type) $this->_backtraces[] = $backtrace; } - $row = array($logs, $backtrace, $type); + $row = [$logs, $backtrace, $type]; $this->_json['rows'][] = $row; $this->_writeHeader($this->_json);