Skip to content

Commit

Permalink
Code readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Nov 6, 2017
1 parent cd15055 commit 980b2b6
Show file tree
Hide file tree
Showing 25 changed files with 99 additions and 82 deletions.
9 changes: 5 additions & 4 deletions system/src/Grav/Common/Backup/ZipBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ZipBackup
/**
* Backup
*
* @param null $destination
* @param string|null $destination
* @param callable|null $messager
*
* @return null|string
Expand Down Expand Up @@ -107,18 +107,19 @@ public static function backup($destination = null, callable $messager = null)
* @param $exclusiveLength
* @param $messager
*/
private static function folderToZip($folder, \ZipArchive &$zipFile, $exclusiveLength, callable $messager = null)
private static function folderToZip($folder, \ZipArchive $zipFile, $exclusiveLength, callable $messager = null)
{
$handle = opendir($folder);
while (false !== $f = readdir($handle)) {
if ($f != '.' && $f != '..') {
if ($f !== '.' && $f !== '..') {
$filePath = "$folder/$f";
// Remove prefix from file path before add to zip.
$localPath = substr($filePath, $exclusiveLength);

if (in_array($f, static::$ignoreFolders)) {
continue;
} elseif (in_array($localPath, static::$ignorePaths)) {
}
if (in_array($localPath, static::$ignorePaths)) {
$zipFile->addEmptyDir($f);
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion system/src/Grav/Common/Config/CompiledBlueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CompiledBlueprints extends CompiledBase
*/
public function checksum()
{
if (!isset($this->checksum)) {
if (null === $this->checksum) {
$this->checksum = md5(json_encode($this->files) . json_encode($this->getTypes()) . $this->version);
}

Expand Down Expand Up @@ -92,6 +92,7 @@ protected function loadFiles()

// Convert file list into parent list.
$list = [];
/** @var array $files */
foreach ($this->files as $files) {
foreach ($files as $name => $item) {
$list[$name][] = $this->path . $item['file'];
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Config/CompiledLanguages.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function loadFile($name, $filename)
{
$file = CompiledYamlFile::instance($filename);
if (preg_match('|languages\.yaml$|', $filename)) {
$this->object->mergeRecursive($file->content());
$this->object->mergeRecursive((array) $file->content());
} else {
$this->object->mergeRecursive([$name => $file->content()]);
}
Expand Down
1 change: 1 addition & 0 deletions system/src/Grav/Common/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

class Config extends Data
{
/** @var string */
protected $checksum;
protected $modified = false;
protected $timestamp = 0;
Expand Down
15 changes: 10 additions & 5 deletions system/src/Grav/Common/Config/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Setup extends Data
*/
public function __construct($container)
{
$environment = isset(static::$environment) ? static::$environment : ($container['uri']->environment() ?: 'localhost');
$environment = null !== static::$environment ? static::$environment : ($container['uri']->environment() ?: 'localhost');

// Pre-load setup.php which contains our initial configuration.
// Configuration may contain dynamic parts, which is why we need to always load it.
Expand All @@ -151,11 +151,13 @@ public function __construct($container)

// Set up environment.
$this->def('environment', $environment ?: 'cli');
$this->def('streams.schemes.environment.prefixes', ['' => ($environment ? ["user://{$this->environment}"] : [])]);
$this->def('streams.schemes.environment.prefixes', ['' => $environment ? ["user://{$this->environment}"] : []]);
}

/**
* @return $this
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function init()
{
Expand All @@ -175,7 +177,7 @@ public function init()
// Update streams.
foreach (array_reverse($files) as $path) {
$file = CompiledYamlFile::instance($path);
$content = $file->content();
$content = (array)$file->content();
if (!empty($content['schemes'])) {
$this->items['streams']['schemes'] = $content['schemes'] + $this->items['streams']['schemes'];
}
Expand All @@ -196,6 +198,7 @@ public function init()
* Initialize resource locator by using the configuration.
*
* @param UniformResourceLocator $locator
* @throws \BadMethodCallException
*/
public function initializeLocator(UniformResourceLocator $locator)
{
Expand All @@ -212,7 +215,7 @@ public function initializeLocator(UniformResourceLocator $locator)
$force = isset($config['force']) ? $config['force'] : false;

if (isset($config['prefixes'])) {
foreach ($config['prefixes'] as $prefix => $paths) {
foreach ((array)$config['prefixes'] as $prefix => $paths) {
$locator->addPath($scheme, $prefix, $paths, $override, $force);
}
}
Expand All @@ -229,7 +232,7 @@ public function getStreams()
$schemes = [];
foreach ((array) $this->get('streams.schemes') as $scheme => $config) {
$type = !empty($config['type']) ? $config['type'] : 'ReadOnlyStream';
if ($type[0] != '\\') {
if ($type[0] !== '\\') {
$type = '\\RocketTheme\\Toolbox\\StreamWrapper\\' . $type;
}

Expand All @@ -242,6 +245,8 @@ public function getStreams()
/**
* @param UniformResourceLocator $locator
* @throws \InvalidArgumentException
* @throws \BadMethodCallException
* @throws \RuntimeException
*/
protected function check(UniformResourceLocator $locator)
{
Expand Down
8 changes: 4 additions & 4 deletions system/src/Grav/Common/Data/BlueprintSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function validateArray(array $data, array $rules)
} elseif (is_array($field) && is_array($val)) {
// Array has been defined in blueprints.
$messages += $this->validateArray($field, $val);
} elseif (isset($rules['validation']) && $rules['validation'] == 'strict') {
} elseif (isset($rules['validation']) && $rules['validation'] === 'strict') {
// Undefined/extra item.
throw new \RuntimeException(sprintf('%s is not defined in blueprints', $key));
}
Expand Down Expand Up @@ -106,7 +106,7 @@ protected function filterArray(array $data, array $rules)
} elseif (is_array($field) && is_array($val)) {
// Array has been defined in blueprints.
$field = $this->filterArray($field, $val);
} elseif (isset($rules['validation']) && $rules['validation'] == 'strict') {
} elseif (isset($rules['validation']) && $rules['validation'] === 'strict') {
$field = null;
}

Expand Down Expand Up @@ -138,7 +138,7 @@ protected function checkRequired(array $data, array $fields)
if (isset($data[$name])) {
continue;
}
if ($field['type'] == 'file' && isset($data['data']['name'][$name])) { //handle case of file input fields required
if ($field['type'] === 'file' && isset($data['data']['name'][$name])) { //handle case of file input fields required
continue;
}

Expand All @@ -164,7 +164,7 @@ protected function dynamicConfig(array &$field, $property, array &$call)
$default = isset($field[$property]) ? $field[$property] : null;
$config = Grav::instance()['config']->get($value, $default);

if (!is_null($config)) {
if (null !== $config) {
$field[$property] = $config;
}
}
Expand Down
4 changes: 2 additions & 2 deletions system/src/Grav/Common/Data/Blueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public function types()

/** @var \DirectoryIterator $file */
foreach ($iterator as $file) {
if (!$file->isFile() || '.' . $file->getExtension() != YAML_EXT) {
if (!$file->isFile() || '.' . $file->getExtension() !== YAML_EXT) {
continue;
}
$name = $file->getBasename(YAML_EXT);
$this->types[$name] = ucfirst(strtr($name, '_', ' '));
$this->types[$name] = ucfirst(str_replace('_', ' ', $name));
}
}

Expand Down
1 change: 1 addition & 0 deletions system/src/Grav/Common/Data/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public function blueprints()

/**
* Save data if storage has been defined.
* @throws \RuntimeException
*/
public function save()
{
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/Errors/SimplePageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function handle()
* @param $resource
*
* @return string
* @throws \RuntimeException
*/
protected function getResource($resource)
{
Expand All @@ -80,16 +81,15 @@ protected function getResource($resource)

// If we got this far, nothing was found.
throw new \RuntimeException(
"Could not find resource '$resource' in any resource paths."
. "(searched: " . join(", ", $this->searchPaths). ")"
"Could not find resource '{$resource}' in any resource paths (searched: " . implode(', ', $this->searchPaths). ')'
);
}

public function addResourcePath($path)
{
if (!is_dir($path)) {
throw new \InvalidArgumentException(
"'$path' is not a valid directory"
"'{$path}' is not a valid directory"
);
}

Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/File/CompiledFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function content($var = null)
// Load real file if cache isn't up to date (or is invalid).
if (
!isset($cache['@class'])
|| $cache['@class'] != $class
|| $cache['modified'] != $modified
|| $cache['filename'] != $this->filename
|| $cache['@class'] !== $class
|| $cache['modified'] !== $modified
|| $cache['filename'] !== $this->filename
) {
// Attempt to lock the file for writing.
try {
Expand Down
19 changes: 10 additions & 9 deletions system/src/Grav/Common/Filesystem/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public static function hashAllFiles($path)
$files[] = $file->getPathname() . '?'. $file->getMTime();
}

$hash = md5(serialize($files));
return $hash;
return md5(serialize($files));
}

/**
Expand Down Expand Up @@ -234,7 +233,7 @@ public static function all($path, array $params = [])
/** @var \RecursiveDirectoryIterator $file */
foreach ($iterator as $file) {
// Ignore hidden files.
if ($file->getFilename()[0] == '.') {
if ($file->getFilename()[0] === '.') {
continue;
}
if (!$folders && $file->isDir()) {
Expand Down Expand Up @@ -339,7 +338,7 @@ public static function move($source, $target)
}

// Don't do anything if the source is the same as the new target
if ($source == $target) {
if ($source === $target) {
return;
}

Expand Down Expand Up @@ -377,6 +376,7 @@ public static function move($source, $target)
* @param string $target
* @param bool $include_target
* @return bool
* @throws \RuntimeException
*/
public static function delete($target, $include_target = true)
{
Expand Down Expand Up @@ -435,6 +435,7 @@ public static function create($folder)
* @param $dest
*
* @return bool
* @throws \RuntimeException
*/
public static function rcopy($src, $dest)
{
Expand All @@ -447,18 +448,18 @@ public static function rcopy($src, $dest)

// If the destination directory does not exist create it
if (!is_dir($dest)) {
Folder::mkdir($dest);
static::mkdir($dest);
}

// Open the source directory to read in files
$i = new \DirectoryIterator($src);
/** @var \DirectoryIterator $f */
foreach ($i as $f) {
if ($f->isFile()) {
copy($f->getRealPath(), "$dest/" . $f->getFilename());
copy($f->getRealPath(), "{$dest}/" . $f->getFilename());
} else {
if (!$f->isDot() && $f->isDir()) {
static::rcopy($f->getRealPath(), "$dest/$f");
static::rcopy($f->getRealPath(), "{$dest}/{$f}");
}
}
}
Expand All @@ -479,10 +480,10 @@ protected static function doDelete($folder, $include_target = true)
}

// Go through all items in filesystem and recursively remove everything.
$files = array_diff(scandir($folder), array('.', '..'));
$files = array_diff(scandir($folder, SCANDIR_SORT_NONE), array('.', '..'));
foreach ($files as $file) {
$path = "{$folder}/{$file}";
(is_dir($path)) ? self::doDelete($path) : @unlink($path);
is_dir($path) ? self::doDelete($path) : @unlink($path);
}

return $include_target ? @rmdir($folder) : true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function accept()
/** @var $current \SplFileInfo */
$current = $this->current();

if ($current->isDir() && !in_array($current->getFilename(), $this::$folder_ignores)) {
if ($current->isDir() && !in_array($current->getFilename(), $this::$folder_ignores, true)) {
return true;
}
return false;
Expand Down
4 changes: 4 additions & 0 deletions system/src/Grav/Common/GPM/Common/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function __isset($key) {
return isset($this->data->$key);
}

public function __set($key, $value) {
throw new \BadMethodCallException('Not Implemented');
}

public function __toString() {
return $this->toJson();
}
Expand Down
7 changes: 4 additions & 3 deletions system/src/Grav/Common/GPM/Remote/GravCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class GravCore extends AbstractPackageCollection
/**
* @param bool $refresh
* @param null $callback
* @throws \InvalidArgumentException
*/
public function __construct($refresh = false, $callback = null)
{
Expand All @@ -38,7 +39,7 @@ public function __construct($refresh = false, $callback = null)
$this->date = isset($this->data['date']) ? $this->data['date'] : '-';

if (isset($this->data['assets'])) {
foreach ($this->data['assets'] as $slug => $data) {
foreach ((array)$this->data['assets'] as $slug => $data) {
$this->items[$slug] = new Package($data);
}
}
Expand Down Expand Up @@ -68,10 +69,10 @@ public function getChangelog($diff = null)
}

$diffLog = [];
foreach ($this->data['changelog'] as $version => $changelog) {
foreach ((array)$this->data['changelog'] as $version => $changelog) {
preg_match("/[\w-\.]+/", $version, $cleanVersion);

if (!$cleanVersion || version_compare($diff, $cleanVersion[0], ">=")) {
if (!$cleanVersion || version_compare($diff, $cleanVersion[0], '>=')) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions system/src/Grav/Common/GPM/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Upgrader
*
* @param boolean $refresh Applies to Remote Packages only and forces a refetch of data
* @param callable $callback Either a function or callback in array notation
* @throws \InvalidArgumentException
*/
public function __construct($refresh = false, $callback = null)
{
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/Helpers/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Base32 {
*/
public static function encode( $bytes ) {
$i = 0; $index = 0; $digit = 0;
$base32 = "";
$base32 = '';
$bytes_len = strlen($bytes);
while( $i < $bytes_len ) {
$currByte = ord($bytes{$i});
Expand All @@ -51,7 +51,7 @@ public static function encode( $bytes ) {
} else {
$digit = ($currByte >> (8 - ($index + 5))) & 0x1F;
$index = ($index + 5) % 8;
if( $index == 0 ) $i++;
if( $index === 0 ) $i++;
}
$base32 .= self::$base32Chars{$digit};
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public static function decode( $base32 ) {
$bytes[$offset] |= $digit << (8 - $index);
}
}
$bites = "";
$bites = '';
foreach( $bytes as $byte ) $bites .= chr($byte);
return $bites;
}
Expand Down
Loading

0 comments on commit 980b2b6

Please sign in to comment.