Skip to content
Snippets Groups Projects
Commit 61cc2268 authored by Alberto Paderno's avatar Alberto Paderno
Browse files

Issue #3461315: Fix the PHP_CodeSniffer issues reported by GitLab CI

parent 31fa8a40
No related branches found
No related tags found
1 merge request!3Issue #3461315: Fix the PHP_CodeSniffer issues reported by GitLab CI
Pipeline #224848 passed
......@@ -27,3 +27,6 @@ include:
# SKIP_ESLINT: '1'
# OPT_IN_TEST_NEXT_MAJOR: '1'
# _CURL_TEMPLATES_REF: 'main'
variables:
# Temporary disable PHPUnit tests.
SKIP_PHPUNIT: '1'
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/apc.iml" filepath="$PROJECT_DIR$/.idea/apc.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MessDetectorOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PHPCSFixerOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PHPCodeSnifferOptionsConfiguration">
<option name="highlightLevel" value="WARNING" />
<option name="transferred" value="true" />
</component>
<component name="PhpIncludePathManager">
<include_path>
<path value="$PROJECT_DIR$/../drupal/includes" />
<path value="$PROJECT_DIR$/../drupal/modules" />
<path value="$PROJECT_DIR$/../drupal/sites/all/modules" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration">
<option name="suggestChangeDefaultLanguageLevel" value="false" />
</component>
<component name="PhpStanOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PsalmOptionsConfiguration">
<option name="transferred" value="true" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?php
/**
* @file
* Drush hook implementations for the Alternative PHP Cache module.
*/
/**
* Implements hook_drush_exit().
*/
function apc_drush_exit() {
if (class_exists('DrupalAPCCache')){
if (class_exists('DrupalAPCCache')) {
DrupalAPCCache::remoteFlush();
}
}
name = APC - Alternative PHP Cache
description = Enables the Alternative PHP Cache.
name = Alternative PHP Cache
description = Integrates the APC extension with a Drupal site.
package = Performance and scalability
core = 7.x
files[] = apc.module
files[] = drupal_apc_cache.inc
files[] = tests/apc.test
<?php
/**
* @file
* Install, update, and uninstall hooks for the Alternative PHP Cache module.
*/
// phpcs:disable SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator.NullCoalesceOperatorNotUsed
/**
* Implements hook_requirements().
*/
function apc_requirements($phase) {
$requirements = array();
// Ensure translations don't break at install time
// Ensure translations don't break at install time.
$t = get_t();
// Test APC.
......
<?php
/**
* @file
* Hook implementations for the Alternative PHP Cache module.
*/
// phpcs:disable SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator.NullCoalesceOperatorNotUsed
/**
* Implements hook_xmlrpc().
*/
function apc_xmlrpc() {
$methods[] = array(
'apc_drush_flush', // Method name.
'apc_drush_flush', // Callback.
$methods[] = array(
'apc_drush_flush',
'apc_drush_flush',
array(
'array', // Return variable.
'array', // Input variable.
'array',
'array',
),
t('XMLRPC callback to enable cache clear from Drush/CLI.'), // Description
t('XMLRPC callback to enable cache clear from Drush/CLI.'),
);
return $methods;
}
/**
* XMLRPC callback to enable cache clear from Drush/CLI.
* XMLRPC callback to clear the cache from Drush/CLI.
*/
function apc_drush_flush($variables) {
$cron_key = isset($variables['cron_key']) ? $variables['cron_key'] : NULL;
......@@ -45,9 +53,7 @@ function apc_drush_flush($variables) {
}
/**
* Implementation of hook_init().
*
* Used for displaying the APC stats for debug purposes.
* Implements hook_init().
*/
function apc_init() {
global $user;
......@@ -62,28 +68,22 @@ function apc_init() {
}
/**
* @file
* This integrates the drupal APC cache module.
*/
/**
* Implementation of hook_permission().
* Implements hook_permission().
*/
function apc_permission() {
return array(
'access apc statistics' => array(
'title' => t('Access apc statistics'),
'description' => t('Allows access to the statistics reports of APC.'),
'access apc statistics' => array(
'title' => t('Access APC statistics'),
'description' => t('Allows access to the APC statistics reports.'),
),
);
}
/**
* See apc_init() which registers this function as a shutdown function.
* Displays apc stats in the footer.
* Shutdown function: Displays APC stats in the footer.
*/
function apc_shutdown() {
global $apc_statistics;
global $_apc_statistics;
// Don't call theme() during shutdown if the registry has been rebuilt (such
// as when enabling/disabling modules on admin/build/modules) as things break.
......@@ -106,11 +106,11 @@ function apc_shutdown() {
}
}
if (isset($apc_statistics) && is_array($apc_statistics)) {
if (isset($_apc_statistics) && is_array($_apc_statistics)) {
print '<div id="apc-devel"><h2>' . t('APC statistics') . '</h2>';
$rows = array();
foreach ($apc_statistics as $row) {
foreach ($_apc_statistics as $row) {
if (is_array($row[2])) {
$row[2] = implode(',<br />', $row[2]);
}
......
<?php
/**
* @file
* This integrates the drupal APC cache backend.
* Functions and classes for the APC cache backend.
*/
// Create dummy functions to prevent fatal errors.
/**
* Dummy functions to prevent errors if APC is not installed.
*/
if (!function_exists('apc_fetch')) {
function apc_fetch($key, $success = NULL) {
/**
* Replacement for apc_fetch() which always return FALSE.
*
* @param string $key
* The key used to store the value.
* @param bool &$success
* A variable set to FALSE.
*
* @return false
* The value has not been fetched.
*/
function apc_fetch($key, &$success = NULL) {
if ($success) {
$success = FALSE;
}
return FALSE;
}
/**
* Replacement for apc_store() which always return FALSE.
*
* @param string $key
* The key used to store the value.
* @param mixed $var
* The value to store.
* @param int $ttl
* The time to live in seconds.
*
* @return false
* The value has not been stored.
*/
function apc_store($key, $var, $ttl = 0) {
return FALSE;
}
}
// Array to store statistics about the current page apc calls.
$apc_statistics = array();
/**
* Statistics about the APC cache.
*/
$_apc_statistics = array();
/**
* APC cache implementation.
*
* This is Drupal's APC cache implementation. It uses Alternative PHP
* Cache to store cached data. Each cache bin corresponds to a prefix of
* the apc variables with the same name.
* This is a Drupal cache implementation which uses the APC extension to store
* cached data.
*/
class DrupalAPCCache implements DrupalCacheInterface {
/**
* The cache bin.
*
* @var string
*/
protected $bin;
/**
* The prefix to use for the cache bin.
*
* @var string
*/
protected $prefix;
/**
* @var boolean
* Whether the class is used by Drush.
*
* @var bool
*/
protected $drush;
/**
* Requests to clear the cache to send via XMLRPC to the server.
*
* @var array
*/
protected static $remoteClears = array();
/**
* Get prefix for bin using the configuration.
* Gets the prefix to use for the cache bin.
*
* @param string $bin
* The cache bin.
*
* @return string
* Can be an empty string, if no prefix set.
* The prefix to use for the cache bin, or an empty string if no prefix has
* been set.
*/
protected static function getPrefixSettingForBin($bin) {
$prefixes = variable_get('cache_prefix', '');
......@@ -66,23 +111,26 @@ protected static function getPrefixSettingForBin($bin) {
if (FALSE !== $prefixes[$bin]) {
// If entry is set and not FALSE, an explicit prefix is set for the bin.
return $prefixes[$bin];
} else {
}
else {
// If we have an explicit false, it means no prefix whatever is the
// default configuration.
return '';
}
} else {
}
else {
// Key is not set, we can safely rely on default behavior.
if (isset($prefixes['default']) && FALSE !== $prefixes['default']) {
return $prefixes['default'];
} else {
}
else {
// When default is not set or an explicit FALSE, this means no prefix.
return '';
}
}
}
function __construct($bin) {
public function __construct($bin) {
$this->bin = $bin;
$this->drush = (drupal_is_cli() && function_exists('drush_log'));
......@@ -96,7 +144,8 @@ function __construct($bin) {
// variable logic and custom module related security logic, that is not
// necessary for all backends.
$prefix = $_SERVER['HTTP_HOST'] . '::';
} else {
}
else {
$prefix = $prefix . '::';
}
......@@ -104,7 +153,7 @@ function __construct($bin) {
if ($test_prefix = drupal_valid_test_ua()) {
$prefix = $test_prefix . '::' . $prefix;
}
else if (isset($GLOBALS['drupal_test_info'])) {
elseif (isset($GLOBALS['drupal_test_info'])) {
$prefix = $GLOBALS['drupal_test_info']['test_run_id'] . '::' . $prefix;
}
......@@ -112,30 +161,35 @@ function __construct($bin) {
}
/**
* Function which retrieves the safe key for the cache bin.
* Gets the key to use for the cache bin in APC calls.
*
* @return
* The safe APC key.
* @return string
* The key to use for the cache bin.
*/
private function binKey() {
return $this->prefix . $this->bin . '::';
}
/**
* Function which retrieves the safe key for the cache cid.
* Gets the key to use in APC calls.
*
* @param $cid
* The cache id.
* @return
* The safe APC key.
* @param string $cid
* The cache ID.
*
* @return string
* The key which can be used in APC calls to avoid conflicts with other
* keys.
*/
private function key($cid) {
return $this->binKey() . $cid;
}
function get($cid) {
/**
* {@inheritdoc}
*/
public function get($cid) {
// Add a get to our statistics.
$GLOBALS['apc_statistics'][] = array('get', $this->bin, array($cid));
$GLOBALS['_apc_statistics'][] = array('get', $this->bin, array($cid));
// Fetch the data.
$cache = apc_fetch($this->key($cid));
......@@ -143,42 +197,53 @@ function get($cid) {
}
/**
* Prepare a cached item.
* Prepares a cached item.
*
* Checks that items are either permanent or did not expire.
*
* @param $cache
* @param object $cache
* An item loaded from cache_get() or cache_get_multiple().
* @return
* The item with data unserialized as appropriate or FALSE if there is no
* valid item to load.
*
* @return mixed
* The item with unserialized data, or FALSE if there is no valid item to
* load.
*/
protected function prepareItem($cache) {
global $user;
if (!isset($cache->data)) {
return FALSE;
}
$temporary = $cache->expire != CACHE_PERMANENT;
$cache_lifetime = variable_get('cache_lifetime', 0);
$expired = (isset($user->cache) && $user->cache > $cache->created);
// If enforcing a minimum cache lifetime, validate that the data is
// currently valid for this user before we return it by making sure the cache
// entry was created before the timestamp in the current session's cache
// timer. The cache variable is loaded into the $user object by _drupal_session_read()
// in session.inc. If the data is permanent or we're not enforcing a minimum
// cache lifetime always return the cached data.
global $user;
if ($cache->expire != CACHE_PERMANENT && variable_get('cache_lifetime', 0) && (isset($user->cache) && $user->cache > $cache->created)) {
// This cache data is too old and thus not valid for us, ignore it.
// currently valid for the currently logged-in user. $user->cache is set by
// _drupal_session_read().
// If the data is permanent, or we are not enforcing a minimum cache
// lifetime, always return the cached data.
if ($temporary && $cache_lifetime && $expired) {
// This cache data is too old and thus not valid for us; ignore it.
return FALSE;
}
return $cache;
}
function getMultiple(&$cids) {
/**
* {@inheritdoc}
*/
public function getMultiple(&$cids) {
$keys = array();
if (!$cids) {
return array();
return $keys;
}
// We need to search the cache with the proper keys and
// be able to get the original $cid back.
// We need to search the cache with the proper keys and be able to get the
// original $cid back.
foreach ($cids as $cid) {
$keys[$this->key($cid)] = $cid;
}
......@@ -186,23 +251,26 @@ function getMultiple(&$cids) {
$fetch = apc_fetch(array_keys($keys));
$cache = array();
if (!empty($fetch)) {
foreach ($fetch as $key => $data) {
$cache[$keys[$key]] = $this->prepareItem($fetch[$key]);
}
foreach ($fetch as $key => $data) {
$cache[$keys[$key]] = $this->prepareItem($data);
}
}
unset($fetch);
// Add a get to our statistics.
$GLOBALS['apc_statistics'][] = array('get', $this->bin, $cids);
$GLOBALS['_apc_statistics'][] = array('get', $this->bin, $cids);
$cids = array_diff($cids, array_keys($cache));
return $cache;
}
function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) {
/**
* {@inheritdoc}
*/
public function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) {
// Add set to statistics.
$GLOBALS['apc_statistics'][] = array('set', $this->bin, $cid);
$GLOBALS['_apc_statistics'][] = array('set', $this->bin, $cid);
// Create new cache object.
$cache = new stdClass();
......@@ -211,7 +279,7 @@ function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) {
$cache->serialized = 0;
$cache->created = REQUEST_TIME;
$cache->expire = $expire;
$cache->headers = isset($headers) ? $headers : NULL;
$cache->headers = $headers;
$cache->data = $data;
......@@ -237,37 +305,42 @@ function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) {
}
/**
* Delete CID matching the given prefix.
* Deletes all the cache IDs matching the given prefix.
*
* @param string $prefix
* The prefix for the cache IDs to delete.
*/
protected function deletePrefix($prefix) {
if (class_exists('APCIterator')) {
$escapedKey = preg_quote($this->binKey() . $prefix, '/');
$iterator = new APCIterator('user', '/^' . $escapedKey . '/', APC_ITER_KEY);
foreach ($iterator as $key => $data) {
apc_delete($key);
}
}
}
}
}
/**
* Flush all cache items in a bin.
* Flushes all cache items in a bin.
*/
function flush() {
protected function flush() {
$this->deletePrefix('');
}
function clear($cid = NULL, $wildcard = FALSE) {
/**
* {@inheritdoc}
*/
public function clear($cid = NULL, $wildcard = FALSE) {
if ($this->drush) {
// APC uses a separate storage for CLI. Send these requests to the server,
// via XMLRPC.
self::$remoteClears[$this->bin][serialize($cid)] = $wildcard;
// APC uses separate storage for CLI mode, bounce the clear request back
// into this method on all server nodes via XMLRPC.
return;
}
// Add a get to our statistics.
$GLOBALS['apc_statistics'][] = array('clear', $this->bin, $cid, (int)$wildcard);
$GLOBALS['_apc_statistics'][] = array('clear', $this->bin, $cid, (int) $wildcard);
if (empty($cid)) {
$this->flush();
......@@ -281,7 +354,7 @@ function clear($cid = NULL, $wildcard = FALSE) {
$this->deletePrefix($cid);
}
}
else if (is_array($cid)) {
elseif (is_array($cid)) {
foreach ($cid as $entry) {
apc_delete($this->key($entry));
}
......@@ -292,7 +365,10 @@ function clear($cid = NULL, $wildcard = FALSE) {
}
}
function isEmpty() {
/**
* {@inheritdoc}
*/
public function isEmpty() {
if (class_exists('APCIterator')) {
$escapedKey = preg_quote($this->binKey(), '/');
$iterator = new APCIterator('user', '/^' . $escapedKey . '/', APC_ITER_KEY);
......@@ -301,15 +377,19 @@ function isEmpty() {
return TRUE;
}
/**
* Flushes the remote cache.
*/
public static function remoteFlush() {
global $base_url;
if (!module_exists('apc')) {
drush_log('You need to enable the APC module for remote cache clearing to work. Run drush pm-enable apc.', 'error');
return;
}
global $base_url;
if (!empty(self::$remoteClears)) {
// optimize '*' clears.
// Optimize '*' clears.
$star = serialize('*');
foreach (self::$remoteClears as $bin => $clears) {
if (!empty($clears[$star])) {
......@@ -317,10 +397,12 @@ public static function remoteFlush() {
}
}
$args = array(
'apc_drush_flush' => array(array(
'clears' => self::$remoteClears,
'cron_key' => variable_get('cron_key', 'drupal'),
)),
'apc_drush_flush' => array(
array(
'clears' => self::$remoteClears,
'cron_key' => variable_get('cron_key', 'drupal'),
),
),
);
$uri = $base_url . '/xmlrpc.php';
$response = xmlrpc($uri, $args);
......@@ -339,4 +421,5 @@ public static function remoteFlush() {
}
}
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment