Browse Source

Minor updates (annotations and best-practices) (#10534)

Combining conditionals and using more succinct idioms.
Travis Rowland 9 years ago
parent
commit
2ea9cce689

+ 2 - 4
src/Auth/DigestAuthenticate.php

@@ -225,10 +225,8 @@ class DigestAuthenticate extends BasicAuthenticate
         ];
 
         $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 = [];

+ 1 - 1
src/Core/ClassLoader.php

@@ -62,7 +62,7 @@ class ClassLoader
         if ($prepend) {
             array_unshift($this->_prefixes[$prefix], $baseDir);
         } else {
-            array_push($this->_prefixes[$prefix], $baseDir);
+            $this->_prefixes[$prefix][] = $baseDir;
         }
     }
 

+ 5 - 5
src/Database/Expression/CaseExpression.php

@@ -126,18 +126,18 @@ class CaseExpression implements ExpressionInterface
                 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;
             }
 
@@ -148,11 +148,11 @@ class CaseExpression implements ExpressionInterface
             }
 
             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];
         }
     }
 

+ 7 - 12
src/Filesystem/File.php

@@ -106,6 +106,7 @@ class File
     public function create()
     {
         $dir = $this->Folder->pwd();
+
         if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
             if (touch($this->path)) {
                 return true;
@@ -127,10 +128,8 @@ class File
         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);
@@ -227,10 +226,8 @@ class File
     {
         $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) {
@@ -620,10 +617,8 @@ class File
             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);

+ 3 - 5
src/Filesystem/Folder.php

@@ -368,7 +368,7 @@ class Folder
      */
     public static function correctSlashFor($path)
     {
-        return (Folder::isWindowsPath($path)) ? '\\' : '/';
+        return Folder::isWindowsPath($path) ? '\\' : '/';
     }
 
     /**
@@ -879,10 +879,8 @@ class Folder
         }
         $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;

+ 1 - 1
src/Http/Response.php

@@ -1735,7 +1735,7 @@ class Response implements ResponseInterface
         } else {
             $result = new DateTime($time);
         }
-        $result->setTimeZone(new DateTimeZone('UTC'));
+        $result->setTimezone(new DateTimeZone('UTC'));
 
         return $result;
     }

+ 4 - 8
src/Network/Socket.php

@@ -317,10 +317,8 @@ class Socket
      */
     public function write($data)
     {
-        if (!$this->connected) {
-            if (!$this->connect()) {
-                return false;
-            }
+        if (!$this->connected && !$this->connect()) {
+            return false;
         }
         $totalBytes = strlen($data);
         $written = 0;
@@ -344,10 +342,8 @@ class Socket
      */
     public function read($length = 1024)
     {
-        if (!$this->connected) {
-            if (!$this->connect()) {
-                return false;
-            }
+        if (!$this->connected && !$this->connect()) {
+            return false;
         }
 
         if (!feof($this->connection)) {

+ 4 - 1
src/ORM/Marshaller.php

@@ -356,6 +356,9 @@ class Marshaller
      * @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 = [])
     {
@@ -379,7 +382,7 @@ class Marshaller
                 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)) {

+ 6 - 1
src/Shell/I18nShell.php

@@ -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()
     {
@@ -80,6 +83,7 @@ class I18nShell extends Shell
      *
      * @param string|null $language Language code to use.
      * @return void
+     * @throws \Cake\Console\Exception\StopException
      */
     public function init($language = null)
     {
@@ -111,7 +115,7 @@ class I18nShell extends Shell
             }
             $filename = $fileinfo->getFilename();
             $newFilename = $fileinfo->getBasename('.pot');
-            $newFilename = $newFilename . '.po';
+            $newFilename .= '.po';
 
             $this->createFile($targetFolder . $newFilename, file_get_contents($sourceFolder . $filename));
             $count++;
@@ -124,6 +128,7 @@ class I18nShell extends Shell
      * Gets the option parser instance and configures it.
      *
      * @return \Cake\Console\ConsoleOptionParser
+     * @throws \Cake\Console\Exception\ConsoleException
      */
     public function getOptionParser()
     {

+ 1 - 1
src/TestSuite/IntegrationTestCase.php

@@ -763,7 +763,7 @@ abstract class IntegrationTestCase extends TestCase
         if (!empty($result)) {
             $message .= ': ' . $result;
         }
-        $this->assertTrue(empty($result), $message);
+        $this->assertEmpty($result, $message);
     }
 
     /**

+ 1 - 1
src/Validation/ValidationRule.php

@@ -169,7 +169,7 @@ class ValidationRule
 
         $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;
             }
         }

+ 1 - 1
src/View/ViewBlock.php

@@ -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;

+ 1 - 1
src/View/Widget/WidgetRegistry.php

@@ -115,7 +115,7 @@ class WidgetRegistry
     public function add(array $widgets)
     {
         foreach ($widgets as $object) {
-            if (gettype($object) === 'object' &&
+            if (is_object($object) &&
                 !($object instanceof WidgetInterface)
             ) {
                 throw new RuntimeException(