Skip to content

Commit

Permalink
Minor updates (annotations and best-practices) (#10534)
Browse files Browse the repository at this point in the history
Combining conditionals and using more succinct idioms.
  • Loading branch information
Theaxiom authored and markstory committed Apr 29, 2017
1 parent 18f8d1f commit 2ea9cce
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 42 deletions.
6 changes: 2 additions & 4 deletions src/Auth/DigestAuthenticate.php
Expand Up @@ -225,10 +225,8 @@ public function loginHeaders(ServerRequest $request)
];

$digest = $this->_getDigest($request);
if ($digest && isset($digest['nonce'])) {
if (!$this->validNonce($digest['nonce'])) {
$options['stale'] = true;
}
if ($digest && isset($digest['nonce']) && !$this->validNonce($digest['nonce'])) {
$options['stale'] = true;
}

$opts = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Core/ClassLoader.php
Expand Up @@ -62,7 +62,7 @@ public function addNamespace($prefix, $baseDir, $prepend = false)
if ($prepend) {
array_unshift($this->_prefixes[$prefix], $baseDir);
} else {
array_push($this->_prefixes[$prefix], $baseDir);
$this->_prefixes[$prefix][] = $baseDir;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Database/Expression/CaseExpression.php
Expand Up @@ -126,18 +126,18 @@ protected function _addExpressions($conditions, $values, $types)
continue;
}

array_push($this->_conditions, $c);
$this->_conditions[] = $c;
$value = isset($rawValues[$k]) ? $rawValues[$k] : 1;

if ($value === 'literal') {
$value = $keyValues[$k];
array_push($this->_values, $value);
$this->_values[] = $value;
continue;
}

if ($value === 'identifier') {
$value = new IdentifierExpression($keyValues[$k]);
array_push($this->_values, $value);
$this->_values[] = $value;
continue;
}

Expand All @@ -148,11 +148,11 @@ protected function _addExpressions($conditions, $values, $types)
}

if ($value instanceof ExpressionInterface) {
array_push($this->_values, $value);
$this->_values[] = $value;
continue;
}

array_push($this->_values, ['value' => $value, 'type' => $type]);
$this->_values[] = ['value' => $value, 'type' => $type];
}
}

Expand Down
19 changes: 7 additions & 12 deletions src/Filesystem/File.php
Expand Up @@ -106,6 +106,7 @@ public function __destruct()
public function create()
{
$dir = $this->Folder->pwd();

if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
if (touch($this->path)) {
return true;
Expand All @@ -127,10 +128,8 @@ public function open($mode = 'r', $force = false)
if (!$force && is_resource($this->handle)) {
return true;
}
if ($this->exists() === false) {
if ($this->create() === false) {
return false;
}
if ($this->exists() === false && $this->create() === false) {
return false;
}

$this->handle = fopen($this->path, $mode);
Expand Down Expand Up @@ -227,10 +226,8 @@ public function write($data, $mode = 'w', $force = false)
{
$success = false;
if ($this->open($mode, $force) === true) {
if ($this->lock !== null) {
if (flock($this->handle, LOCK_EX) === false) {
return false;
}
if ($this->lock !== null && flock($this->handle, LOCK_EX) === false) {
return false;
}

if (fwrite($this->handle, $data) !== false) {
Expand Down Expand Up @@ -620,10 +617,8 @@ public function replaceText($search, $replace)
return false;
}

if ($this->lock !== null) {
if (flock($this->handle, LOCK_EX) === false) {
return false;
}
if ($this->lock !== null && flock($this->handle, LOCK_EX) === false) {
return false;
}

$replaced = $this->write(str_replace($search, $replace, $this->read()), 'w', true);
Expand Down
8 changes: 3 additions & 5 deletions src/Filesystem/Folder.php
Expand Up @@ -368,7 +368,7 @@ public static function normalizePath($path)
*/
public static function correctSlashFor($path)
{
return (Folder::isWindowsPath($path)) ? '\\' : '/';
return Folder::isWindowsPath($path) ? '\\' : '/';
}

/**
Expand Down Expand Up @@ -879,10 +879,8 @@ public function move($options)
}
$options += ['to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => [], 'recursive' => true];

if ($this->copy($options)) {
if ($this->delete($options['from'])) {
return (bool)$this->cd($options['to']);
}
if ($this->copy($options) && $this->delete($options['from'])) {
return (bool)$this->cd($options['to']);
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Response.php
Expand Up @@ -1735,7 +1735,7 @@ protected function _getUTCDate($time = null)
} else {
$result = new DateTime($time);
}
$result->setTimeZone(new DateTimeZone('UTC'));
$result->setTimezone(new DateTimeZone('UTC'));

return $result;
}
Expand Down
12 changes: 4 additions & 8 deletions src/Network/Socket.php
Expand Up @@ -317,10 +317,8 @@ public function setLastError($errNum, $errStr)
*/
public function write($data)
{
if (!$this->connected) {
if (!$this->connect()) {
return false;
}
if (!$this->connected && !$this->connect()) {
return false;
}
$totalBytes = strlen($data);
$written = 0;
Expand All @@ -344,10 +342,8 @@ public function write($data)
*/
public function read($length = 1024)
{
if (!$this->connected) {
if (!$this->connect()) {
return false;
}
if (!$this->connected && !$this->connect()) {
return false;
}

if (!feof($this->connection)) {
Expand Down
5 changes: 4 additions & 1 deletion src/ORM/Marshaller.php
Expand Up @@ -356,6 +356,9 @@ public function many(array $data, array $options = [])
* @param array $data The data to convert into entities.
* @param array $options List of options.
* @return array An array of built entities.
* @throws \BadMethodCallException
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
protected function _belongsToMany(Association $assoc, array $data, $options = [])
{
Expand All @@ -379,7 +382,7 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
if (count($keys) === $primaryCount) {
$rowConditions = [];
foreach ($keys as $key => $value) {
$rowConditions[][$target->aliasfield($key)] = $value;
$rowConditions[][$target->aliasField($key)] = $value;
}

if ($forceNew && !$target->exists($rowConditions)) {
Expand Down
7 changes: 6 additions & 1 deletion src/Shell/I18nShell.php
Expand Up @@ -43,6 +43,9 @@ class I18nShell extends Shell
* Override main() for help message hook
*
* @return void
* @throws \InvalidArgumentException
* @throws \Cake\Core\Exception\MissingPluginException
* @throws \Cake\Console\Exception\StopException
*/
public function main()
{
Expand Down Expand Up @@ -80,6 +83,7 @@ public function main()
*
* @param string|null $language Language code to use.
* @return void
* @throws \Cake\Console\Exception\StopException
*/
public function init($language = null)
{
Expand Down Expand Up @@ -111,7 +115,7 @@ public function init($language = null)
}
$filename = $fileinfo->getFilename();
$newFilename = $fileinfo->getBasename('.pot');
$newFilename = $newFilename . '.po';
$newFilename .= '.po';

$this->createFile($targetFolder . $newFilename, file_get_contents($sourceFolder . $filename));
$count++;
Expand All @@ -124,6 +128,7 @@ public function init($language = null)
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
* @throws \Cake\Console\Exception\ConsoleException
*/
public function getOptionParser()
{
Expand Down
2 changes: 1 addition & 1 deletion src/TestSuite/IntegrationTestCase.php
Expand Up @@ -763,7 +763,7 @@ public function assertNoRedirect($message = '')
if (!empty($result)) {
$message .= ': ' . $result;
}
$this->assertTrue(empty($result), $message);
$this->assertEmpty($result, $message);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Validation/ValidationRule.php
Expand Up @@ -169,7 +169,7 @@ protected function _skip($context)

$newRecord = $context['newRecord'];
if (!empty($this->_on)) {
if ($this->_on === 'create' && !$newRecord || $this->_on === 'update' && $newRecord) {
if (($this->_on === 'create' && !$newRecord) || ($this->_on === 'update' && $newRecord)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/View/ViewBlock.php
Expand Up @@ -86,7 +86,7 @@ class ViewBlock
*/
public function start($name, $mode = ViewBlock::OVERRIDE)
{
if (in_array($name, array_keys($this->_active))) {
if (array_key_exists($name, $this->_active)) {
throw new Exception(sprintf("A view block with the name '%s' is already/still open.", $name));
}
$this->_active[$name] = $mode;
Expand Down
2 changes: 1 addition & 1 deletion src/View/Widget/WidgetRegistry.php
Expand Up @@ -115,7 +115,7 @@ public function load($file)
public function add(array $widgets)
{
foreach ($widgets as $object) {
if (gettype($object) === 'object' &&
if (is_object($object) &&
!($object instanceof WidgetInterface)
) {
throw new RuntimeException(
Expand Down

0 comments on commit 2ea9cce

Please sign in to comment.