Skip to content

Commit

Permalink
fix code review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rosasurfer committed Jul 14, 2024
1 parent 89ccdae commit dc52be1
Show file tree
Hide file tree
Showing 27 changed files with 86 additions and 76 deletions.
1 change: 0 additions & 1 deletion bin/logwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use rosasurfer\config\Config;
use rosasurfer\exception\IllegalTypeException;
use rosasurfer\net\mail\Mailer;
use rosasurfer\util\PHP;

use function rosasurfer\echoPre;
use function rosasurfer\stderror;
Expand Down
14 changes: 8 additions & 6 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(array $options = []) {

if (isSet($_GET['__phpinfo__']) || isSet($_GET['__config__']) || isSet($_GET['__cache__'])) {
if (self::isAdminIP()) {
foreach ($_GET as $param => $value) {
foreach (\array_keys($_GET) as $param) {
if ($param == '__phpinfo__') {
if ($configInfoTask) {
$phpInfoTask = false;
Expand Down Expand Up @@ -182,7 +182,7 @@ public function run(array $options = []) {
private function configurePhp() {
register_shutdown_function(function() {
$warnLimit = php_byte_value(Config::getDefault()->get('log.warn.memory_limit', PHP_INT_MAX));
$usedBytes = memory_get_peak_usage($real=true);
$usedBytes = memory_get_peak_usage(true);
if ($usedBytes > $warnLimit) {
Logger::log('Memory consumption exceeded '.prettyBytes($warnLimit).' (peak usage: '.prettyBytes($usedBytes).')', L_WARN, ['class' => __CLASS__]);
}
Expand Down Expand Up @@ -231,7 +231,7 @@ private function loadConfiguration(array $options) {
if (!$config->get('app.dir.config', false)) {
$files = $config->getMonitoredFiles();
end($files);
list($file, $exists) = each($files);
$file = key($files);
$config->set('app.dir.config', dirname($file));
}
unset($options['app.config']);
Expand Down Expand Up @@ -279,15 +279,17 @@ private function expandAppDirs(IConfig $config, $rootDir) {
* @param string $rootDir - application root directory
*/
private function expandDirsRecursive(array &$dirs, $rootDir) {
foreach ($dirs as $name => &$dir) {
foreach ($dirs as &$dir) {
if (is_array($dir)) {
$this->{__FUNCTION__}($dir, $rootDir);
continue;
}
if (isRelativePath($dir))
if (isRelativePath($dir)) {
$dir = $rootDir.'/'.$dir;
}
if (is_dir($dir)) $dir = realpath($dir);
}; unset($dir);
}
unset($dir);
}


Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function registerClassLoader() {
case ClassLoader ::class: require(MINISTRUTS_ROOT.'/src/loader/ClassLoader.php'); break;
}
};
spl_autoload_register($bootstrap, $throw=true, $prepend=true);
spl_autoload_register($bootstrap, true, true);

// instantiate and register the framework's class loader
$loader = new ClassLoader();
Expand All @@ -59,7 +59,7 @@ function registerClassLoader() {

// register an otherwise lost legacy auto-loader
if ($legacyAutoLoad && spl_autoload_functions()[0]!='__autoload') {
spl_autoload_register('__autoload', $throw=true, $prepend=true);
spl_autoload_register('__autoload', true, true);
}
}
registerClassLoader();
Expand Down
2 changes: 1 addition & 1 deletion src/cache/ReferencePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function set($key, &$value, $expires = Cache::EXPIRES_NEVER, Dependency $
if (!is_int($expires)) throw new IllegalTypeException('Illegal type of parameter $expires: '.gettype($expires));

// im Cache wird ein Array[creation_timestamp, value, expires, dependency] gespeichert
$this->pool[$key] = array($timestamp=null, $value, $expires=null, $dependency);
$this->pool[$key] = array(null, $value, null, $dependency);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/AutoConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct($location) {

// set "app.dir.config" to the directory of the most recently added file
end($this->files);
list($file, $isFile) = each($this->files);
$file = key($this->files);
$this->set('app.dir.config', dirname($file));


Expand Down
8 changes: 5 additions & 3 deletions src/config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ public function dump(array $options = null) {
}
}
$value = str_pad($key, $maxKeyLength, ' ', STR_PAD_RIGHT).' = '.$value;
}; unset($value);
}
unset($value);
$lines += $values;

$padLeft = isSet($options['pad-left']) ? $options['pad-left'] : '';
Expand Down Expand Up @@ -575,7 +576,7 @@ public function export(array $options = null) {
}
}

foreach ($values as $key => &$value) {
foreach ($values as &$value) {
// convert special values to their string representation
if (is_null($value)) $value = '(null)';
elseif (is_bool($value)) $value = ($value ? '(true)' : '(false)');
Expand All @@ -598,7 +599,8 @@ public function export(array $options = null) {
$value = '"'.$value.'"';
}
}
}; unset($value);
}
unset($value);

return $values;
}
Expand Down
3 changes: 2 additions & 1 deletion src/db/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public static function getConnector($id = null) {
if (!$options) throw new IllegalStateException('No configuration found for database alias "'.$id.'"');

// resolve the class name to use for the connector
$className = $options['connector']; unset($options['connector']);
$className = $options['connector'];
unset($options['connector']);
$className = str_replace('/', '\\', $className);
if ($className[0]=='\\') $className = substr($className, 1);

Expand Down
7 changes: 4 additions & 3 deletions src/db/mysql/MySQLConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ public function connect() {
$pass = $this->password;

// connect
try { // flags: CLIENT_FOUND_ROWS = 2
try { // flags: CLIENT_FOUND_ROWS = 2
$php_errormsg = '';
$this->hConnection = mysql_connect($host, $user, $pass, $newLink=true/*, $flags=2*/);
$this->hConnection = mysql_connect($host, $user, $pass, true/*, $flags=2*/);
!$this->hConnection && trigger_error($php_errormsg, E_USER_ERROR);
}
catch (\Exception $ex) {
Expand Down Expand Up @@ -316,7 +316,8 @@ public function escapeIdentifier($name) {

foreach ($names as &$subname) {
$subname = '`'.str_replace('`', '``', $subname).'`';
}; unset($subname);
}
unset($subname);

return join('.', $names);
}
Expand Down
2 changes: 1 addition & 1 deletion src/db/orm/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ protected function parseMapping(array $mapping) {
$mapping['getters'][$getter] = &$property;
$mapping['relations'][$name] = &$property;
unset($mapping['relations'][$i], $property);
};
}
}
else $mapping['relations'] = [];

Expand Down
7 changes: 4 additions & 3 deletions src/db/orm/PersistableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function __sleep() {
$mapping = $this->dao()->getMapping();
$array = (array) $this;

foreach ($mapping['relations'] as $name => $property) {
foreach (\array_keys($mapping['relations']) as $name) {
if (is_object($this->$name)) {
/** @var PersistableObject $object */
$object = $this->$name;
Expand Down Expand Up @@ -425,7 +425,7 @@ private function insert() {

// collect column values
$values = [];
foreach ($mapping['columns'] as $column => $property) {
foreach (\array_keys($mapping['columns']) as $column) {
$values[$column] = $this->getPhysicalValue($column);
};

Expand Down Expand Up @@ -526,7 +526,8 @@ private function doInsert(array $values) {
// translate column values
foreach ($values as &$value) {
$value = $db->escapeLiteral($value);
}; unset($value);
}
unset($value);

// create SQL statement
$sql = 'insert into '.$table.' ('.join(', ', \array_keys($values)).')
Expand Down
4 changes: 2 additions & 2 deletions src/db/orm/meta/EntityMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getProperty($name) {
*/
public function getIdentity() {
if ($this->identity === null) {
foreach ($this->mapping['properties'] as $name => $property) {
foreach ($this->mapping['properties'] as $property) {
if (isSet($property['primary']) && $property['primary']===true) {
return $this->identity = new PropertyMapping($this, $property);
}
Expand All @@ -109,7 +109,7 @@ public function getIdentity() {
*/
public function getVersion() {
if ($this->version === null) {
foreach ($this->mapping['properties'] as $name => $property) {
foreach ($this->mapping['properties'] as $property) {
if (isSet($property['version']) && $property['version']===true) {
return $this->version = new PropertyMapping($this, $property);
}
Expand Down
3 changes: 2 additions & 1 deletion src/db/pgsql/PostgresConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ public function escapeIdentifier($name) {

foreach ($names as &$subname) {
$subname = pg_escape_identifier($this->hConnection, $subname);
}; unset($subname);
}
unset($subname);

return join('.', $names);
}
Expand Down
4 changes: 2 additions & 2 deletions src/debug/DebugHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static function formatTrace(array $trace, $indent = '') {
for ($i=0; $i < $size; $i++) { // align FILE and LINE
$frame = &$trace[$i];

$call = self::getFQFunctionName($frame, $nsLowerCase=true);
$call = self::getFQFunctionName($frame, true);

if ($call!='{main}' && !strEndsWith($call, '{closure}'))
$call.='()';
Expand Down Expand Up @@ -271,7 +271,7 @@ public static function errorLevelToStr($level) {
else if (($level & E_ALL) == E_ALL) $levels = ['E_ALL']; // 32767
else if (($level & (E_ALL & ~E_DEPRECATED)) == (E_ALL & ~E_DEPRECATED)) $levels = ['E_ALL & ~E_DEPRECATED']; // 24575
else {
foreach ($levels as $key => $value) {
foreach (\array_keys($levels) as $key) {
if ($level & $key) continue;
unset($levels[$key]);
}
Expand Down
38 changes: 19 additions & 19 deletions src/debug/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function setupExceptionHandling() {
public static function handleError($level, $message, $file, $line, array $context = null) {
//echoPre(__METHOD__.'() '.DebugHelper::errorLevelToStr($level).': $message='.$message.', $file='.$file.', $line='.$line);

// (1) Ignore suppressed errors and errors not covered by the current reporting level.
// Ignore suppressed errors and errors not covered by the current reporting level.
$reportingLevel = error_reporting();
if (!$reportingLevel) return false; // the @ operator was specified
if (!($reportingLevel & $level)) return true; // the error is not covered by current reporting level
Expand All @@ -161,7 +161,7 @@ public static function handleError($level, $message, $file, $line, array $contex
$logContext['file'] = $file;
$logContext['line'] = $line;

// (2) Process errors according to their severity level.
// Process errors according to their severity level.
switch ($level) {
// log non-critical errors and continue normally
case E_DEPRECATED : return true(Logger::log($message, L_INFO, $logContext));
Expand All @@ -170,30 +170,30 @@ public static function handleError($level, $message, $file, $line, array $contex
case E_USER_WARNING : return true(Logger::log($message, L_WARN, $logContext));
}

// (3) Wrap everything else in the matching PHPError exception.
// Wrap everything else in the matching PHPError exception.
switch ($level) {
case E_PARSE : $exception = new PHPParseError ($message, $code=null, $severity=$level, $file, $line); break;
case E_COMPILE_WARNING : $exception = new PHPCompileWarning ($message, $code=null, $severity=$level, $file, $line); break;
case E_COMPILE_ERROR : $exception = new PHPCompileError ($message, $code=null, $severity=$level, $file, $line); break;
case E_CORE_WARNING : $exception = new PHPCoreWarning ($message, $code=null, $severity=$level, $file, $line); break;
case E_CORE_ERROR : $exception = new PHPCoreError ($message, $code=null, $severity=$level, $file, $line); break;
case E_STRICT : $exception = new PHPStrict ($message, $code=null, $severity=$level, $file, $line); break;
case E_NOTICE : $exception = new PHPNotice ($message, $code=null, $severity=$level, $file, $line); break;
case E_WARNING : $exception = new PHPWarning ($message, $code=null, $severity=$level, $file, $line); break;
case E_ERROR : $exception = new PHPError ($message, $code=null, $severity=$level, $file, $line); break;
case E_RECOVERABLE_ERROR: $exception = new PHPRecoverableError($message, $code=null, $severity=$level, $file, $line); break;
case E_USER_ERROR : $exception = new PHPUserError ($message, $code=null, $severity=$level, $file, $line); break;

default : $exception = new PHPUnknownError ($message, $code=null, $severity=$level, $file, $line);
case E_PARSE : $exception = new PHPParseError ($message, null, $level, $file, $line); break;
case E_COMPILE_WARNING : $exception = new PHPCompileWarning ($message, null, $level, $file, $line); break;
case E_COMPILE_ERROR : $exception = new PHPCompileError ($message, null, $level, $file, $line); break;
case E_CORE_WARNING : $exception = new PHPCoreWarning ($message, null, $level, $file, $line); break;
case E_CORE_ERROR : $exception = new PHPCoreError ($message, null, $level, $file, $line); break;
case E_STRICT : $exception = new PHPStrict ($message, null, $level, $file, $line); break;
case E_NOTICE : $exception = new PHPNotice ($message, null, $level, $file, $line); break;
case E_WARNING : $exception = new PHPWarning ($message, null, $level, $file, $line); break;
case E_ERROR : $exception = new PHPError ($message, null, $level, $file, $line); break;
case E_RECOVERABLE_ERROR: $exception = new PHPRecoverableError($message, null, $level, $file, $line); break;
case E_USER_ERROR : $exception = new PHPUserError ($message, null, $level, $file, $line); break;

default : $exception = new PHPUnknownError ($message, null, $level, $file, $line);
}

// (4) Handle the error according to the error configuration.
// Handle the error according to the error configuration.
if (self::$errorMode == self::LOG_ERRORS) {
Logger::log($exception, L_ERROR, $logContext);
return true;
}

// (5) Handle cases where throwing an exception is not possible or not allowed.
// Handle cases where throwing an exception is not possible or not allowed.

/**
* Errors triggered by require() or require_once()
Expand All @@ -214,7 +214,7 @@ public static function handleError($level, $message, $file, $line, array $contex
}
}

// (6) Throw back everything else.
// Throw back everything else.
throw $exception;
}

Expand Down
2 changes: 1 addition & 1 deletion src/exception/error/PHPError.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PHPError extends \ErrorException implements IRosasurferException {
* @param int $line - the line number in the file where the error occurred
*/
public function __construct($message, $code, $severity, $file, $line) {
parent::__construct($message, $code, $severity, $file, $line, $cause=null);
parent::__construct($message, $code, $severity, $file, $line, null);
}


Expand Down
Loading

0 comments on commit dc52be1

Please sign in to comment.