From 8773577b192526add63a82b407fa1c51bab38be7 Mon Sep 17 00:00:00 2001 From: Colin Mollenhour Date: Wed, 12 Apr 2023 20:23:46 -0400 Subject: [PATCH] Fix code style with PHP CS Fixer --- .gitignore | 1 + .php-cs-fixer.dist.php | 2 +- File.php | 77 +++++++++++++++++------------ tests/CommonBackendTest.php | 8 ++- tests/CommonExtendedBackendTest.php | 6 +-- tests/FileBackendTest.php | 12 +++-- 6 files changed, 60 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index e9cef75..b608165 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea vendor composer.lock +.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index a1945d5..271c605 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -12,7 +12,7 @@ ->setFinder( PhpCsFixer\Finder::create() ->in([ - 'Cm/', + './', 'tests/', ]) ->name('*.php') diff --git a/File.php b/File.php index e3fd161..99ae029 100644 --- a/File.php +++ b/File.php @@ -37,7 +37,6 @@ */ class Cm_Cache_Backend_File extends Zend_Cache_Backend_File { - /** @var array */ protected $_options = array( 'cache_dir' => null, // Path to cache files @@ -116,7 +115,9 @@ public function __construct(array $options = array()) */ public function setDirectives($directives) { - if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array'); + if (!is_array($directives)) { + Zend_Cache::throwException('Directives parameter must be an array'); + } foreach ($directives as $name => $value) { if (!is_string($name)) { Zend_Cache::throwException("Incorrect option name : $name"); @@ -142,11 +143,11 @@ public function load($id, $doNotTestCacheValidity = false) { $file = $this->_file($id); $cache = $this->_getCache($file, true); - if ( ! $cache) { + if (! $cache) { return false; } list($metadatas, $data) = $cache; - if ( ! $doNotTestCacheValidity && (time() > $metadatas['expire'])) { + if (! $doNotTestCacheValidity && (time() > $metadatas['expire'])) { // ?? $this->remove($id); return false; } @@ -322,7 +323,7 @@ public function getMetadatas($id) { $metadatas = $this->_getCache($this->_file($id), false); if ($metadatas) { - $metadatas['tags'] = explode(',' ,$metadatas['tags']); + $metadatas['tags'] = explode(',', $metadatas['tags']); } return $metadatas; } @@ -366,19 +367,25 @@ protected function _getCache($file, $withData) if (!is_file($file) || ! ($fd = @fopen($file, 'rb'))) { return false; } - if ($this->_options['file_locking']) flock($fd, LOCK_SH); + if ($this->_options['file_locking']) { + flock($fd, LOCK_SH); + } $metadata = fgets($fd); - if ( ! $metadata) { - if ($this->_options['file_locking']) flock($fd, LOCK_UN); + if (! $metadata) { + if ($this->_options['file_locking']) { + flock($fd, LOCK_UN); + } fclose($fd); return false; } if ($withData) { $data = stream_get_contents($fd); } - if ($this->_options['file_locking']) flock($fd, LOCK_UN); + if ($this->_options['file_locking']) { + flock($fd, LOCK_UN); + } fclose($fd); - $metadata = @unserialize(rtrim($metadata,"\n"), ['allowed_classes' => false]); + $metadata = @unserialize(rtrim($metadata, "\n"), ['allowed_classes' => false]); if ($metadata === false) { return false; } @@ -431,8 +438,8 @@ protected function _path($id, $parts = false) $root .= $prefix . '--' . substr(md5($id), -$this->_options['hashed_directory_level']) . DIRECTORY_SEPARATOR; $partsArray[] = $root; } - if ($parts){ - return $partsArray; + if ($parts) { + return $partsArray; } return $root; @@ -467,7 +474,7 @@ protected function _clean($dir, $mode = Zend_Cache::CLEANING_MODE_ALL, $tags = a if ($glob === false) { return true; } - foreach ($glob as $file) { + foreach ($glob as $file) { if (is_file($file)) { if ($mode == Zend_Cache::CLEANING_MODE_ALL) { $result = @unlink($file) && $result; @@ -481,7 +488,7 @@ protected function _clean($dir, $mode = Zend_Cache::CLEANING_MODE_ALL, $tags = a continue; } $metadatas = $this->_getCache($file, false); - if ( ! $metadatas) { + if (! $metadatas) { @unlink($file); continue; } @@ -534,8 +541,7 @@ protected function _cleanNew($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = arra { $result = true; $ids = $this->_getIdsByTags($mode, $tags, true); - switch($mode) - { + switch($mode) { case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG: case Zend_Cache::CLEANING_MODE_MATCHING_TAG: $this->_updateIdsTags($ids, $tags, 'diff'); @@ -564,7 +570,7 @@ protected function _getIdsByTags($mode, $tags, $delete) $ids = $this->getIds(); if ($tags) { foreach ($tags as $tag) { - if ( ! $ids) { + if (! $ids) { break; // early termination optimization } $ids = array_diff($ids, $this->_getTagIds($tag)); @@ -576,7 +582,7 @@ protected function _getIdsByTags($mode, $tags, $delete) $tag = array_shift($tags); $ids = $this->_getTagIds($tag); foreach ($tags as $tag) { - if ( ! $ids) { + if (! $ids) { break; // early termination optimization } $ids = array_intersect($ids, $this->_getTagIds($tag)); @@ -587,16 +593,20 @@ protected function _getIdsByTags($mode, $tags, $delete) case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG: foreach ($tags as $tag) { $file = $this->_tagFile($tag); - if ( ! is_file($file) || ! ($fd = @fopen($file, 'rb+'))) { + if (! is_file($file) || ! ($fd = @fopen($file, 'rb+'))) { continue; } - if ($this->_options['file_locking']) flock($fd, LOCK_EX); - $ids = array_merge($ids,$this->_getTagIds($fd)); + if ($this->_options['file_locking']) { + flock($fd, LOCK_EX); + } + $ids = array_merge($ids, $this->_getTagIds($fd)); if ($delete) { fseek($fd, 0); ftruncate($fd, 0); } - if ($this->_options['file_locking']) flock($fd, LOCK_UN); + if ($this->_options['file_locking']) { + flock($fd, LOCK_UN); + } fclose($fd); } $ids = array_unique($ids); @@ -626,8 +636,8 @@ protected function _tagFile($id) protected function _tagPath() { $path = $this->_options['cache_dir'] . DIRECTORY_SEPARATOR . $this->_options['file_name_prefix']. '-tags' . DIRECTORY_SEPARATOR; - if ( ! $this->_isTagDirChecked) { - if ( ! is_dir($path)) { + if (! $this->_isTagDirChecked) { + if (! is_dir($path)) { if (@mkdir($path, $this->_options['use_chmod'] ? $this->_options['directory_mode'] : 0777) && $this->_options['use_chmod']) { @chmod($path, $this->_options['directory_mode']); // see #ZF-320 (this line is required in some configurations) } @@ -650,7 +660,7 @@ protected function _getTagIds($tag) } else { $ids = false; } - if( ! $ids) { + if(! $ids) { return array(); } $ids = trim(substr($ids, 0, strrpos($ids, "\n"))); @@ -672,13 +682,15 @@ protected function _updateIdsTags($ids, $tags, $mode) foreach($tags as $tag) { $file = $this->_tagFile($tag); if (file_exists($file)) { - if ($mode == 'diff' || (mt_rand(1,100) == 1 && filesize($file) > 4096)) { + if ($mode == 'diff' || (mt_rand(1, 100) == 1 && filesize($file) > 4096)) { $file = $this->_tagFile($tag); - if ( ! ($fd = @fopen($file, 'rb+'))) { + if (! ($fd = @fopen($file, 'rb+'))) { $result = false; continue; } - if ($this->_options['file_locking']) flock($fd, LOCK_EX); + if ($this->_options['file_locking']) { + flock($fd, LOCK_EX); + } if ($mode == 'diff') { $_ids = array_diff($this->_getTagIds($fd), $ids); } else { @@ -687,13 +699,14 @@ protected function _updateIdsTags($ids, $tags, $mode) fseek($fd, 0); ftruncate($fd, 0); $result = fwrite($fd, implode("\n", array_unique($_ids))."\n") && $result; - if ($this->_options['file_locking']) flock($fd, LOCK_UN); + if ($this->_options['file_locking']) { + flock($fd, LOCK_UN); + } fclose($fd); - } - else { + } else { $result = file_put_contents($file, implode("\n", $ids)."\n", FILE_APPEND | ($this->_options['file_locking'] ? LOCK_EX : 0)) && $result; } - } else if ($mode == 'merge') { + } elseif ($mode == 'merge') { $result = $this->_filePutContents($file, implode("\n", $ids)."\n") && $result; } } diff --git a/tests/CommonBackendTest.php b/tests/CommonBackendTest.php index 88647f6..91788b3 100644 --- a/tests/CommonBackendTest.php +++ b/tests/CommonBackendTest.php @@ -34,8 +34,8 @@ class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase'); * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_Cache */ -abstract class Zend_Cache_CommonBackendTest extends \PHPUnit\Framework\TestCase { - +abstract class Zend_Cache_CommonBackendTest extends \PHPUnit\Framework\TestCase +{ protected $_instance; protected $_className; protected $_root; @@ -83,7 +83,7 @@ public function getTmpDir($date = true) } if (is_writeable($this->_root)) { return $this->_root . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix; - } else if (getenv('TMPDIR')){ + } elseif (getenv('TMPDIR')) { return getenv('TMPDIR') . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix; } else { die("no writable tmpdir found"); @@ -281,5 +281,3 @@ public function testCleanModeNotMatchingTags3() } } - - diff --git a/tests/CommonExtendedBackendTest.php b/tests/CommonExtendedBackendTest.php index 6ed82a8..750b639 100644 --- a/tests/CommonExtendedBackendTest.php +++ b/tests/CommonExtendedBackendTest.php @@ -33,8 +33,8 @@ * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_Cache */ -abstract class Zend_Cache_CommonExtendedBackendTest extends Zend_Cache_CommonBackendTest { - +abstract class Zend_Cache_CommonExtendedBackendTest extends Zend_Cache_CommonBackendTest +{ private $_capabilities; public function __construct($name = null, array $data = array(), $dataName = '') @@ -210,5 +210,3 @@ public function testGetCapabilities() } } - - diff --git a/tests/FileBackendTest.php b/tests/FileBackendTest.php index 183950f..9792ba2 100644 --- a/tests/FileBackendTest.php +++ b/tests/FileBackendTest.php @@ -32,8 +32,8 @@ * @copyright Copyright (c) 2012 Colin Mollenhour (http://colin.mollenhour.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class Cm_Cache_FileBackendTest extends Zend_Cache_CommonExtendedBackendTest { - +class Cm_Cache_FileBackendTest extends Zend_Cache_CommonExtendedBackendTest +{ protected $_instance; protected $_cache_dir; @@ -57,8 +57,12 @@ public function tearDown(): void unset($this->_instance); } - public function testConstructorBadOption() { } - public function testConstructorCorrectCall() { } + public function testConstructorBadOption() + { + } + public function testConstructorCorrectCall() + { + } public function testGetWithANonExistingCacheIdAndANullLifeTime() {