Skip to content

Commit

Permalink
Fix code style with PHP CS Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmollenhour committed Apr 13, 2023
1 parent 2d4e864 commit 8773577
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
.idea
vendor
composer.lock
.php-cs-fixer.cache
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Expand Up @@ -12,7 +12,7 @@
->setFinder(
PhpCsFixer\Finder::create()
->in([
'Cm/',
'./',
'tests/',
])
->name('*.php')
Expand Down
77 changes: 45 additions & 32 deletions File.php
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")));
Expand All @@ -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 {
Expand All @@ -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;
}
}
Expand Down
8 changes: 3 additions & 5 deletions tests/CommonBackendTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -281,5 +281,3 @@ public function testCleanModeNotMatchingTags3()
}

}


6 changes: 2 additions & 4 deletions tests/CommonExtendedBackendTest.php
Expand Up @@ -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 = '')
Expand Down Expand Up @@ -210,5 +210,3 @@ public function testGetCapabilities()
}

}


12 changes: 8 additions & 4 deletions tests/FileBackendTest.php
Expand Up @@ -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;

Expand All @@ -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()
{
Expand Down

0 comments on commit 8773577

Please sign in to comment.