Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: use null coalescing operator #359

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions lib/action/sfAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function getPartial($templateName, $vars = null)
{
$this->getContext()->getConfiguration()->loadHelpers('Partial');

$vars = null !== $vars ? $vars : $this->varHolder->getAll();
$vars ??= $this->varHolder->getAll();
connorhu marked this conversation as resolved.
Show resolved Hide resolved

return get_partial($templateName, $vars);
}
Expand Down Expand Up @@ -333,7 +333,7 @@ public function getComponent($moduleName, $componentName, $vars = null)
{
$this->getContext()->getConfiguration()->loadHelpers('Partial');

$vars = null !== $vars ? $vars : $this->varHolder->getAll();
$vars ??= $this->varHolder->getAll();

return get_component($moduleName, $componentName, $vars);
}
Expand Down Expand Up @@ -388,15 +388,7 @@ public function getSecurityValue($name, $default = null)
{
$actionName = strtolower($this->getActionName());

if (isset($this->security[$actionName][$name])) {
return $this->security[$actionName][$name];
}

if (isset($this->security['all'][$name])) {
return $this->security['all'][$name];
}

return $default;
return $this->security[$actionName][$name] ?? $this->security['all'][$name] ?? $default;
}

/**
Expand Down Expand Up @@ -430,7 +422,7 @@ public function getCredential()
public function setTemplate($name, $module = null)
{
if (sfConfig::get('sf_logging_enabled')) {
$this->dispatcher->notify(new sfEvent($this, 'application.log', [sprintf('Change template to "%s/%s"', null === $module ? 'CURRENT' : $module, $name)]));
$this->dispatcher->notify(new sfEvent($this, 'application.log', [sprintf('Change template to "%s/%s"', $module ?? 'CURRENT', $name)]));
}

if (null !== $module) {
Expand Down Expand Up @@ -516,6 +508,6 @@ public function getRoute()
*/
protected function get404Message($message = null)
{
return null === $message ? sprintf('This request has been forwarded to a 404 error page by the action "%s/%s".', $this->getModuleName(), $this->getActionName()) : $message;
return $message ?? sprintf('This request has been forwarded to a 404 error page by the action "%s/%s".', $this->getModuleName(), $this->getActionName());
}
}
25 changes: 3 additions & 22 deletions lib/action/sfActionStack.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ public function addEntry($moduleName, $actionName, $actionInstance)
*/
public function getEntry($index)
{
$retval = null;

if ($index > -1 && $index < count($this->stack)) {
$retval = $this->stack[$index];
}

return $retval;
return $this->stack[$index] ?? null;
}

/**
Expand All @@ -75,13 +69,7 @@ public function popEntry()
*/
public function getFirstEntry()
{
$retval = null;

if (isset($this->stack[0])) {
$retval = $this->stack[0];
}

return $retval;
return $this->stack[0] ?? null;
}

/**
Expand All @@ -91,14 +79,7 @@ public function getFirstEntry()
*/
public function getLastEntry()
{
$count = count($this->stack);
$retval = null;

if (isset($this->stack[0])) {
$retval = $this->stack[$count - 1];
}

return $retval;
return $this->stack[count($this->stack) - 1] ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/autoload/sfAutoload.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getClassPath($class)
{
$class = strtolower($class);

return isset($this->classes[$class]) ? $this->classes[$class] : null;
return $this->classes[$class] ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/autoload/sfSimpleAutoload.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function getClassPath($class)
{
$class = strtolower($class);

return isset($this->classes[$class]) ? $this->classes[$class] : null;
return $this->classes[$class] ?? null;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/cache/sfCache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function getMany($keys)
*/
public function getLifetime($lifetime)
{
return null === $lifetime ? $this->getOption('lifetime') : $lifetime;
return $lifetime ?? $this->getOption('lifetime');
}

/**
Expand All @@ -191,7 +191,7 @@ public function getBackend()
*/
public function getOption($name, $default = null)
{
return isset($this->options[$name]) ? $this->options[$name] : $default;
return $this->options[$name] ?? $default;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/cache/sfMemcacheCache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function initialize($options = [])

if ($this->getOption('servers')) {
foreach ($this->getOption('servers') as $server) {
$port = isset($server['port']) ? $server['port'] : 11211;
if (!$this->memcache->addServer($server['host'], $port, isset($server['persistent']) ? $server['persistent'] : true)) {
$port = $server['port'] ?? 11211;
if (!$this->memcache->addServer($server['host'], $port, $server['persistent'] ?? true)) {
throw new sfInitializationException(sprintf('Unable to connect to the memcache server (%s:%s).', $server['host'], $port));
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public function has($key)
*/
public function set($key, $data, $lifetime = null)
{
$lifetime = null === $lifetime ? $this->getOption('lifetime') : $lifetime;
$lifetime ??= $this->getOption('lifetime');

// save metadata
$this->setMetadata($key, $lifetime);
Expand Down
6 changes: 3 additions & 3 deletions lib/cache/sfSQLiteCache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function get($key, $default = null)
$data = $this->dbh->singleQuery(sprintf("SELECT data FROM cache WHERE key = '%s' AND timeout > %d", sqlite_escape_string($key), time()));
}

return null === $data ? $default : $data;
return $data ?? $default;
}

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public function getTimeout($key)
if ($this->isSqLite3()) {
$rs = $this->dbh->querySingle(sprintf("SELECT timeout FROM cache WHERE key = '%s' AND timeout > %d", $this->dbh->escapeString($key), time()));

return null === $rs ? 0 : $rs;
return $rs ?? 0;
}

$rs = $this->dbh->query(sprintf("SELECT timeout FROM cache WHERE key = '%s' AND timeout > %d", sqlite_escape_string($key), time()));
Expand All @@ -169,7 +169,7 @@ public function getLastModified($key)
if ($this->isSqLite3()) {
$rs = $this->dbh->querySingle(sprintf("SELECT last_modified FROM cache WHERE key = '%s' AND timeout > %d", $this->dbh->escapeString($key), time()));

return null === $rs ? 0 : $rs;
return $rs ?? 0;
}

$rs = $this->dbh->query(sprintf("SELECT last_modified FROM cache WHERE key = '%s' AND timeout > %d", sqlite_escape_string($key), time()));
Expand Down
12 changes: 6 additions & 6 deletions lib/command/sfCommandApplication.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ abstract class sfCommandApplication
public function __construct(sfEventDispatcher $dispatcher, ?sfFormatter $formatter = null, $options = [])
{
$this->dispatcher = $dispatcher;
$this->formatter = null === $formatter ? $this->guessBestFormatter(STDOUT) : $formatter;
$this->formatter = $formatter ?? $this->guessBestFormatter(STDOUT);
$this->options = $options;

$this->fixCgi();
Expand Down Expand Up @@ -100,7 +100,7 @@ abstract public function configure();
*/
public function getOption($name)
{
return isset($this->options[$name]) ? $this->options[$name] : null;
return $this->options[$name] ?? null;
}

/**
Expand Down Expand Up @@ -396,11 +396,11 @@ public function renderException($e)
]);

for ($i = 0, $count = count($trace); $i < $count; ++$i) {
$class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
$type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
$class = $trace[$i]['class'] ?? '';
$type = $trace[$i]['type'] ?? '';
$function = $trace[$i]['function'];
$file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
$line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
$file = $trace[$i]['file'] ?? 'n/a';
$line = $trace[$i]['line'] ?? 'n/a';

fwrite(STDERR, sprintf(" %s%s%s at %s:%s\n", $class, $type, $function, $this->formatter->format($file, 'INFO', STDERR), $this->formatter->format($line, 'INFO', STDERR)));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/command/sfCommandLogger.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function initialize(sfEventDispatcher $dispatcher, $options = [])
*/
public function listenToLogEvent(sfEvent $event)
{
$priority = isset($event['priority']) ? $event['priority'] : self::INFO;
$priority = $event['priority'] ?? self::INFO;

$prefix = '';
if ('application.log' == $event->getName()) {
Expand Down
6 changes: 3 additions & 3 deletions lib/config/sfAutoloadConfigHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ protected function parse(array $configFiles)
}
} else {
// directory mapping
$ext = isset($entry['ext']) ? $entry['ext'] : '.php';
$ext = $entry['ext'] ?? '.php';
$path = $entry['path'];

// we automatically add our php classes
require_once sfConfig::get('sf_symfony_lib_dir').'/util/sfFinder.class.php';
$finder = sfFinder::type('file')->name('*'.$ext)->follow_link();

// recursive mapping?
$recursive = isset($entry['recursive']) ? $entry['recursive'] : false;
$recursive = $entry['recursive'] ?? false;
if (!$recursive) {
$finder->maxdepth(0);
}
Expand All @@ -154,7 +154,7 @@ protected function parse(array $configFiles)

if ($matches = glob($path)) {
foreach ($finder->in($matches) as $file) {
$mapping = array_merge($mapping, $this->parseFile($path, $file, isset($entry['prefix']) ? $entry['prefix'] : ''));
$mapping = array_merge($mapping, $this->parseFile($path, $file, $entry['prefix'] ?? ''));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/config/sfConfig.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class sfConfig
*/
public static function get($name, $default = null)
{
return isset(self::$config[$name]) ? self::$config[$name] : $default;
return self::$config[$name] ?? $default;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/config/sfFactoryConfigHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function execute($configFiles)
$defaultParameters = [];
$defaultParameters[] = sprintf("'auto_shutdown' => false, 'session_id' => \$this->getRequest()->getParameter('%s'),", $parameters['session_name']);
if (is_subclass_of($class, 'sfDatabaseSessionStorage')) {
$defaultParameters[] = sprintf("'database' => \$this->getDatabaseManager()->getDatabase('%s'),", isset($parameters['database']) ? $parameters['database'] : 'default');
$defaultParameters[] = sprintf("'database' => \$this->getDatabaseManager()->getDatabase('%s'),", $parameters['database'] ?? 'default');
unset($parameters['database']);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/config/sfFilterConfigHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function execute($configFiles)
unset($keys['param']['condition']);
}

$type = isset($keys['param']['type']) ? $keys['param']['type'] : null;
$type = $keys['param']['type'] ?? null;
unset($keys['param']['type']);

if ($condition) {
Expand Down
8 changes: 4 additions & 4 deletions lib/config/sfGeneratorConfigHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ public function execute($configFiles)
}

if (!isset($config['generator'])) {
throw new sfParseException(sprintf('Configuration file "%s" must specify a generator section.', isset($configFiles[1]) ? $configFiles[1] : $configFiles[0]));
throw new sfParseException(sprintf('Configuration file "%s" must specify a generator section.', $configFiles[1] ?? $configFiles[0]));
}

$config = $config['generator'];

if (!isset($config['class'])) {
throw new sfParseException(sprintf('Configuration file "%s" must specify a generator class section under the generator section.', isset($configFiles[1]) ? $configFiles[1] : $configFiles[0]));
throw new sfParseException(sprintf('Configuration file "%s" must specify a generator class section under the generator section.', $configFiles[1] ?? $configFiles[0]));
}

foreach (['fields', 'list', 'edit'] as $section) {
if (isset($config[$section])) {
throw new sfParseException(sprintf('Configuration file "%s" can specify a "%s" section but only under the param section.', isset($configFiles[1]) ? $configFiles[1] : $configFiles[0], $section));
throw new sfParseException(sprintf('Configuration file "%s" can specify a "%s" section but only under the param section.', $configFiles[1] ?? $configFiles[0], $section));
}
}

// generate class and add a reference to it
$generatorManager = new sfGeneratorManager(sfContext::getInstance()->getConfiguration());

// generator parameters
$generatorParam = (isset($config['param']) ? $config['param'] : []);
$generatorParam = ($config['param'] ?? []);

// hack to find the module name (look for the last /modules/ in path)
preg_match('#.*/modules/([^/]+)/#', str_replace('\\', '/', $configFiles[0]), $match);
Expand Down
2 changes: 1 addition & 1 deletion lib/config/sfPluginConfiguration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(sfProjectConfiguration $configuration, $rootDir = nu
$this->configuration = $configuration;
$this->dispatcher = $configuration->getEventDispatcher();
$this->rootDir = null === $rootDir ? $this->guessRootDir() : realpath($rootDir);
$this->name = null === $name ? $this->guessName() : $name;
$this->name = $name ?? $this->guessName();

$this->setup();
$this->configure();
Expand Down
2 changes: 1 addition & 1 deletion lib/config/sfProjectConfiguration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct($rootDir = null, ?sfEventDispatcher $dispatcher = nu

$this->rootDir = null === $rootDir ? static::guessRootDir() : realpath($rootDir);
$this->symfonyLibDir = realpath(__DIR__.'/..');
$this->dispatcher = null === $dispatcher ? new sfEventDispatcher() : $dispatcher;
$this->dispatcher = $dispatcher ?? new sfEventDispatcher();

ini_set('magic_quotes_runtime', 'off');

Expand Down
14 changes: 7 additions & 7 deletions lib/config/sfRoutingConfigHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ protected function parse($configFiles)
(isset($params['type']) && 'collection' == $params['type'])
|| (isset($params['class']) && false !== strpos($params['class'], 'Collection'))
) {
$options = isset($params['options']) ? $params['options'] : [];
$options = $params['options'] ?? [];
$options['name'] = $name;
$options['requirements'] = isset($params['requirements']) ? $params['requirements'] : [];
$options['requirements'] = $params['requirements'] ?? [];

$routes[$name] = [isset($params['class']) ? $params['class'] : 'sfRouteCollection', [$options]];
$routes[$name] = [$params['class'] ?? 'sfRouteCollection', [$options]];
} else {
$routes[$name] = [isset($params['class']) ? $params['class'] : 'sfRoute', [
$routes[$name] = [$params['class'] ?? 'sfRoute', [
$params['url'] ?: '/',
isset($params['params']) ? $params['params'] : (isset($params['param']) ? $params['param'] : []),
isset($params['requirements']) ? $params['requirements'] : [],
isset($params['options']) ? $params['options'] : [],
$params['params'] ?? $params['param'] ?? [],
$params['requirements'] ?? [],
$params['options'] ?? [],
]];
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/config/sfYamlConfigHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function parseYaml($configFile)
throw new sfParseException(sprintf('Configuration file "%s" could not be parsed', $configFile));
}

return null === $config ? [] : $config;
return $config ?? [];
}

public static function flattenConfiguration($config)
Expand Down
4 changes: 2 additions & 2 deletions lib/debug/sfWebDebug.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function removePanel($name)
*/
public function getOption($name, $default = null)
{
return isset($this->options[$name]) ? $this->options[$name] : $default;
return $this->options[$name] ?? $default;
}

/**
Expand Down Expand Up @@ -178,7 +178,7 @@ public function injectToolbar($content)
*/
public function asHtml()
{
$current = isset($this->options['request_parameters']['sfWebDebugPanel']) ? $this->options['request_parameters']['sfWebDebugPanel'] : null;
$current = $this->options['request_parameters']['sfWebDebugPanel'] ?? null;

$titles = [];
$panels = [];
Expand Down
Loading
Loading