Browse Source

Merge branch '4.x' into 4.x-allow-empty-null

ADmad 6 years ago
parent
commit
6c2b594591

+ 6 - 0
.travis.yml

@@ -64,7 +64,13 @@ script:
   - |
       if [[ $CHECKS == 1 ]]; then
         composer stan-setup
+      fi
+  - |
+      if [[ $CHECKS == 1 ]]; then
         composer stan
+      fi
+  - |
+      if [[ $CHECKS == 1 ]]; then
         composer cs-check
       fi
 

+ 1 - 1
src/Auth/BaseAuthenticate.php

@@ -66,7 +66,7 @@ abstract class BaseAuthenticate implements EventListenerInterface
     /**
      * Password hasher instance.
      *
-     * @var \Cake\Auth\AbstractPasswordHasher
+     * @var \Cake\Auth\AbstractPasswordHasher|null
      */
     protected $_passwordHasher;
 

+ 1 - 1
src/Command/CompletionCommand.php

@@ -186,7 +186,7 @@ class CompletionCommand extends Command implements CommandCollectionAwareInterfa
      * Reflect the subcommands names out of a shell.
      *
      * @param \Cake\Console\Shell $shell The shell to get commands for
-     * @return array A list of commands
+     * @return string[] A list of commands
      */
     protected function shellSubcommands(Shell $shell): array
     {

+ 3 - 3
src/Command/I18nExtractCommand.php

@@ -41,14 +41,14 @@ class I18nExtractCommand extends Command
     /**
      * Paths to use when looking for strings
      *
-     * @var array
+     * @var string[]
      */
     protected $_paths = [];
 
     /**
      * Files from where to extract
      *
-     * @var array
+     * @var string[]
      */
     protected $_files = [];
 
@@ -104,7 +104,7 @@ class I18nExtractCommand extends Command
     /**
      * An array of directories to exclude.
      *
-     * @var array
+     * @var string[]
      */
     protected $_exclude = [];
 

+ 1 - 1
src/Command/RoutesGenerateCommand.php

@@ -64,7 +64,7 @@ class RoutesGenerateCommand extends Command
      * Split the CLI arguments into a hash.
      *
      * @param string[] $args The arguments to split.
-     * @return array
+     * @return (string|bool)[]
      */
     protected function _splitArgs(array $args): array
     {

+ 1 - 1
src/Controller/ComponentRegistry.php

@@ -56,7 +56,7 @@ class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterfa
     /**
      * Get the controller associated with the collection.
      *
-     * @return \Cake\Controller\Controller Controller instance
+     * @return \Cake\Controller\Controller Controller instance or null if not set.
      */
     public function getController(): Controller
     {

+ 1 - 1
src/Database/DriverInterface.php

@@ -133,7 +133,7 @@ interface DriverInterface
      * Returns whether the driver supports adding or dropping constraints
      * to already created tables.
      *
-     * @return bool true if driver supports dynamic constraints.
+     * @return bool True if driver supports dynamic constraints.
      */
     public function supportsDynamicConstraints(): bool;
 

+ 1 - 1
src/TestSuite/Stub/ConsoleInput.php

@@ -30,7 +30,7 @@ class ConsoleInput extends ConsoleInputBase
     /**
      * Reply values for ask() and askChoice()
      *
-     * @var array
+     * @var string[]
      */
     protected $replies = [];
 

+ 13 - 1
src/Validation/Validator.php

@@ -678,7 +678,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      * method called will take precedence.
      *
      * @deprecated 3.7.0 Use allowEmptyString(), allowEmptyArray(), allowEmptyFile(),
-     *   allowEmptyDate(), allowEmptyTime() or allowEmptyDateTime() instead.
+     *   allowEmptyDate(), allowEmptyTime() allowEmptyDateTime() or allowEmptyFor() instead.
      * @param string|array $field the name of the field or a list of fields
      * @param bool|string|callable $when Indicates when the field is allowed to be empty
      * Valid values are true (always), 'create', 'update'. If a callable is passed then
@@ -688,6 +688,12 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      */
     public function allowEmpty($field, $when = true, $message = null)
     {
+        deprecationWarning(
+            'allowEmpty() is deprecated. '
+            . 'Use allowEmptyString(), allowEmptyArray(), allowEmptyFile(), allowEmptyDate(), allowEmptyTime(), '
+            . 'allowEmptyDateTime() or allowEmptyFor() instead.'
+        );
+
         $defaults = [
             'when' => $when,
             'message' => $message,
@@ -1139,6 +1145,12 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      */
     public function notEmpty($field, ?string $message = null, $when = false)
     {
+        deprecationWarning(
+            'notEmpty() is deprecated. '
+            . 'Use notEmptyString(), notEmptyArray(), notEmptyFile(), notEmptyDate(), notEmptyTime() '
+            . 'or notEmptyDateTime() instead.'
+        );
+
         $defaults = [
             'when' => $when,
             'message' => $message,

+ 1 - 1
src/View/View.php

@@ -222,7 +222,7 @@ class View implements EventDispatcherInterface
     /**
      * Default custom config options.
      *
-     * @var array
+     * @var string[]
      */
     protected $_defaultConfig = [];
 

+ 3 - 0
tests/TestCase/Routing/Route/RouteTest.php

@@ -1526,6 +1526,9 @@ class RouteTest extends TestCase
         $route = new Route('/{controller}/{action}');
         $this->assertSame('_controller:_action', $route->getName());
 
+        $route = new Route('/{controller}/{action}');
+        $this->assertSame('_controller:_action', $route->getName());
+
         $route = new Route('/articles/:action', ['controller' => 'posts']);
         $this->assertSame('posts:_action', $route->getName());
 

+ 79 - 73
tests/TestCase/Validation/ValidatorTest.php

@@ -62,7 +62,7 @@ class ValidatorTest extends TestCase
         $this->assertSame('This field cannot be left empty', $validator->getNotEmptyMessage('field'));
 
         $validator = new Validator();
-        $validator->notEmpty('field', 'Custom message');
+        $validator->notEmptyString('field', 'Custom message');
         $this->assertSame('Custom message', $validator->getNotEmptyMessage('field'));
 
         $validator = new Validator();
@@ -70,7 +70,7 @@ class ValidatorTest extends TestCase
         $this->assertSame('Cannot be blank', $validator->getNotEmptyMessage('field'));
 
         $validator = new Validator();
-        $validator->notEmpty('field', 'Cannot be empty');
+        $validator->notEmptyString('field', 'Cannot be empty');
         $validator->notBlank('field', 'Cannot be blank');
         $this->assertSame('Cannot be blank', $validator->getNotEmptyMessage('field'));
     }
@@ -431,7 +431,7 @@ class ValidatorTest extends TestCase
     {
         $validator = new Validator();
         $validator->requirePresence('id', 'update');
-        $validator->allowEmpty('id', 'create');
+        $validator->allowEmptyString('id', 'create');
         $validator->requirePresence('title');
 
         $data = [
@@ -649,13 +649,13 @@ class ValidatorTest extends TestCase
     public function testAllowEmpty()
     {
         $validator = new Validator();
-        $this->assertSame($validator, $validator->allowEmpty('title'));
+        $this->assertSame($validator, $validator->allowEmptyString('title'));
         $this->assertTrue($validator->field('title')->isEmptyAllowed());
 
-        $validator->allowEmpty('title', 'create');
+        $validator->allowEmptyString('title', null, 'create');
         $this->assertSame('create', $validator->field('title')->isEmptyAllowed());
 
-        $validator->allowEmpty('title', 'update');
+        $validator->allowEmptyString('title', null, 'update');
         $this->assertSame('update', $validator->field('title')->isEmptyAllowed());
     }
 
@@ -667,7 +667,7 @@ class ValidatorTest extends TestCase
     public function testAllowEmptyWithDateTimeFields()
     {
         $validator = new Validator();
-        $validator->allowEmpty('created')
+        $validator->allowEmptyDate('created')
             ->add('created', 'date', ['rule' => 'date']);
 
         $data = [
@@ -694,6 +694,7 @@ class ValidatorTest extends TestCase
         $result = $validator->errors($data);
         $this->assertEmpty($result, 'No errors on empty datetime');
 
+        $validator->allowEmptyTime('created');
         $data = [
             'created' => [
                 'hour' => '',
@@ -713,7 +714,7 @@ class ValidatorTest extends TestCase
     public function testAllowEmptyWithFileFields()
     {
         $validator = new Validator();
-        $validator->allowEmpty('picture')
+        $validator->allowEmptyFile('picture')
             ->add('picture', 'file', ['rule' => 'uploadedFile']);
 
         $data = [
@@ -758,20 +759,23 @@ class ValidatorTest extends TestCase
     {
         $validator = new Validator();
 
-        $validator->allowEmpty([
-            'title',
-            'subject',
-            'posted_at' => [
-                'when' => false,
-                'message' => 'Post time cannot be empty',
-            ],
-            'updated_at' => [
-                'when' => true,
-            ],
-            'show_at' => [
-                'when' => Validator::WHEN_UPDATE,
-            ],
-        ], 'create', 'Cannot be empty');
+        $this->deprecated(function () use ($validator) {
+            $validator->allowEmpty([
+                'title',
+                'subject',
+                'posted_at' => [
+                    'when' => false,
+                    'message' => 'Post time cannot be empty',
+                ],
+                'updated_at' => [
+                    'when' => true,
+                ],
+                'show_at' => [
+                    'when' => Validator::WHEN_UPDATE,
+                ],
+            ], 'create', 'Cannot be empty');
+        });
+
         $this->assertSame('create', $validator->field('title')->isEmptyAllowed());
         $this->assertSame('create', $validator->field('subject')->isEmptyAllowed());
         $this->assertFalse($validator->field('posted_at')->isEmptyAllowed());
@@ -801,9 +805,11 @@ class ValidatorTest extends TestCase
      */
     public function testAllowEmptyAsArrayFailure()
     {
-        $this->expectException(\InvalidArgumentException::class);
-        $validator = new Validator();
-        $validator->allowEmpty(['title' => 'derp', 'created' => false]);
+        $this->deprecated(function () {
+            $this->expectException(\InvalidArgumentException::class);
+            $validator = new Validator();
+            $validator->allowEmpty(['title' => 'derp', 'created' => false]);
+        });
     }
 
     /**
@@ -1589,10 +1595,10 @@ class ValidatorTest extends TestCase
     public function testNotEmpty()
     {
         $validator = new Validator();
-        $validator->notEmpty('title');
+        $validator->notEmptyString('title');
         $this->assertFalse($validator->field('title')->isEmptyAllowed());
 
-        $validator->allowEmpty('title');
+        $validator->allowEmptyString('title');
         $this->assertTrue($validator->field('title')->isEmptyAllowed());
     }
 
@@ -1604,26 +1610,28 @@ class ValidatorTest extends TestCase
     public function testNotEmptyAsArray()
     {
         $validator = new Validator();
-        $validator->notEmpty(['title', 'created']);
+        $validator->notEmptyString('title')->notEmptyString('created');
         $this->assertFalse($validator->field('title')->isEmptyAllowed());
         $this->assertFalse($validator->field('created')->isEmptyAllowed());
 
-        $validator->notEmpty([
-            'title' => [
-                'when' => false,
-            ],
-            'content' => [
-                'when' => Validator::WHEN_UPDATE,
-            ],
-            'posted_at' => [
-                'when' => Validator::WHEN_CREATE,
-            ],
-            'show_at' => [
-                'message' => 'Show date cannot be empty',
-                'when' => false,
-            ],
-            'subject',
-        ], 'Not empty', true);
+        $this->deprecated(function () use ($validator) {
+            $validator->notEmpty([
+                'title' => [
+                    'when' => false,
+                ],
+                'content' => [
+                    'when' => Validator::WHEN_UPDATE,
+                ],
+                'posted_at' => [
+                    'when' => Validator::WHEN_CREATE,
+                ],
+                'show_at' => [
+                    'message' => 'Show date cannot be empty',
+                    'when' => false,
+                ],
+                'subject',
+            ], 'Not empty', true);
+        });
 
         $this->assertFalse($validator->field('title')->isEmptyAllowed());
         $this->assertTrue($validator->isEmptyAllowed('content', true));
@@ -1655,9 +1663,11 @@ class ValidatorTest extends TestCase
      */
     public function testNotEmptyAsArrayFailure()
     {
-        $this->expectException(\InvalidArgumentException::class);
-        $validator = new Validator();
-        $validator->notEmpty(['title' => 'derp', 'created' => false]);
+        $this->deprecated(function () {
+            $this->expectException(\InvalidArgumentException::class);
+            $validator = new Validator();
+            $validator->notEmpty(['title' => 'derp', 'created' => false]);
+        });
     }
 
     /**
@@ -1668,19 +1678,19 @@ class ValidatorTest extends TestCase
     public function testNotEmptyModes()
     {
         $validator = new Validator();
-        $validator->notEmpty('title', 'Need a title', 'create');
+        $validator->notEmptyString('title', 'Need a title', 'create');
         $this->assertFalse($validator->isEmptyAllowed('title', true));
         $this->assertTrue($validator->isEmptyAllowed('title', false));
 
-        $validator->notEmpty('title', 'Need a title', 'update');
+        $validator->notEmptyString('title', 'Need a title', 'update');
         $this->assertTrue($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
 
-        $validator->notEmpty('title', 'Need a title');
+        $validator->notEmptyString('title', 'Need a title');
         $this->assertFalse($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
 
-        $validator->notEmpty('title');
+        $validator->notEmptyString('title');
         $this->assertFalse($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
     }
@@ -1693,18 +1703,18 @@ class ValidatorTest extends TestCase
     public function testNotEmptyAndIsAllowed()
     {
         $validator = new Validator();
-        $validator->allowEmpty('title')
-            ->notEmpty('title', 'Need it', 'update');
+        $validator->allowEmptyString('title')
+            ->notEmptyString('title', 'Need it', 'update');
         $this->assertTrue($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
 
-        $validator->allowEmpty('title')
-            ->notEmpty('title');
+        $validator->allowEmptyString('title')
+            ->notEmptyString('title');
         $this->assertFalse($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
 
-        $validator->notEmpty('title')
-            ->allowEmpty('title', 'create');
+        $validator->notEmptyString('title')
+            ->allowEmptyString('title', null, 'create');
         $this->assertTrue($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
     }
@@ -1718,7 +1728,7 @@ class ValidatorTest extends TestCase
     {
         $validator = new Validator();
         $allow = true;
-        $validator->allowEmpty('title', function ($context) use (&$allow) {
+        $validator->allowEmptyString('title', null, function ($context) use (&$allow) {
             $this->assertEquals([], $context['data']);
             $this->assertEquals([], $context['providers']);
             $this->assertTrue($context['newRecord']);
@@ -1740,7 +1750,7 @@ class ValidatorTest extends TestCase
     {
         $validator = new Validator();
         $prevent = true;
-        $validator->notEmpty('title', 'error message', function ($context) use (&$prevent) {
+        $validator->notEmptyString('title', 'error message', function ($context) use (&$prevent) {
             $this->assertEquals([], $context['data']);
             $this->assertEquals([], $context['providers']);
             $this->assertFalse($context['newRecord']);
@@ -1761,19 +1771,19 @@ class ValidatorTest extends TestCase
     public function testIsEmptyAllowed()
     {
         $validator = new Validator();
-        $this->assertSame($validator, $validator->allowEmpty('title'));
+        $this->assertSame($validator, $validator->allowEmptyString('title'));
         $this->assertTrue($validator->isEmptyAllowed('title', true));
         $this->assertTrue($validator->isEmptyAllowed('title', false));
 
-        $validator->notEmpty('title');
+        $validator->notEmptyString('title');
         $this->assertFalse($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
 
-        $validator->allowEmpty('title', 'create');
+        $validator->allowEmptyString('title', null, 'create');
         $this->assertTrue($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
 
-        $validator->allowEmpty('title', 'update');
+        $validator->allowEmptyString('title', null, 'update');
         $this->assertTrue($validator->isEmptyAllowed('title', false));
         $this->assertFalse($validator->isEmptyAllowed('title', true));
     }
@@ -1786,15 +1796,11 @@ class ValidatorTest extends TestCase
     public function testErrorsWithEmptyNotAllowed()
     {
         $validator = new Validator();
-        $validator->notEmpty('title');
+        $validator->notEmptyString('title');
         $errors = $validator->errors(['title' => '']);
         $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
         $this->assertEquals($expected, $errors);
 
-        $errors = $validator->errors(['title' => []]);
-        $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
-        $this->assertEquals($expected, $errors);
-
         $errors = $validator->errors(['title' => null]);
         $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
         $this->assertEquals($expected, $errors);
@@ -1817,7 +1823,7 @@ class ValidatorTest extends TestCase
     public function testCustomErrorsWithAllowedEmpty()
     {
         $validator = new Validator();
-        $validator->allowEmpty('title', false, 'Custom message');
+        $validator->allowEmptyString('title', 'Custom message', false);
         $errors = $validator->errors(['title' => null]);
         $expected = ['title' => ['_empty' => 'Custom message']];
         $this->assertEquals($expected, $errors);
@@ -1831,7 +1837,7 @@ class ValidatorTest extends TestCase
     public function testCustomErrorsWithEmptyNotAllowed()
     {
         $validator = new Validator();
-        $validator->notEmpty('title', 'Custom message');
+        $validator->notEmptyString('title', 'Custom message');
         $errors = $validator->errors(['title' => '']);
         $expected = ['title' => ['_empty' => 'Custom message']];
         $this->assertEquals($expected, $errors);
@@ -1845,7 +1851,7 @@ class ValidatorTest extends TestCase
     public function testErrorsWithEmptyAllowed()
     {
         $validator = new Validator();
-        $validator->allowEmpty('title');
+        $validator->allowEmptyString('title');
         $errors = $validator->errors(['title' => '']);
         $this->assertEmpty($errors);
 
@@ -2174,7 +2180,7 @@ class ValidatorTest extends TestCase
         $validator->setProvider('test', $this);
         $validator->add('title', 'not-empty', ['rule' => 'notBlank']);
         $validator->requirePresence('body');
-        $validator->allowEmpty('published');
+        $validator->allowEmptyString('published');
 
         $result = $validator->__debugInfo();
         $expected = [
@@ -2199,7 +2205,7 @@ class ValidatorTest extends TestCase
             '_presenceMessages' => [],
             '_allowEmptyMessages' => [],
             '_allowEmptyFlags' => [
-                'published' => Validator::EMPTY_ALL,
+                'published' => Validator::EMPTY_STRING,
             ],
             '_useI18n' => true,
         ];

+ 4 - 4
tests/TestCase/View/Form/EntityContextTest.php

@@ -883,7 +883,7 @@ class EntityContextTest extends TestCase
         $articles = $this->getTableLocator()->get('Articles');
 
         $validator = $articles->getValidator();
-        $validator->notEmpty('title', 'nope', function ($context) {
+        $validator->notEmptyString('title', 'nope', function ($context) {
             return $context['providers']['entity']->isRequired();
         });
         $articles->setValidator('default', $validator);
@@ -911,7 +911,7 @@ class EntityContextTest extends TestCase
 
         $comments = $this->getTableLocator()->get('Comments');
         $validator = $comments->getValidator();
-        $validator->allowEmpty('comment', function ($context) {
+        $validator->allowEmptyString('comment', null, function ($context) {
             return $context['providers']['entity']->isNew();
         });
 
@@ -1395,13 +1395,13 @@ class EntityContextTest extends TestCase
         ]);
 
         $validator = new Validator();
-        $validator->notEmpty('title', 'Don\'t forget a title!');
+        $validator->notEmptyString('title', 'Don\'t forget a title!');
         $validator->add('title', 'minlength', [
             'rule' => ['minlength', 10],
         ])
         ->add('body', 'maxlength', [
             'rule' => ['maxlength', 1000],
-        ])->allowEmpty('body');
+        ])->allowEmptyString('body');
         $articles->setValidator('create', $validator);
 
         $validator = new Validator();

+ 1 - 1
tests/TestCase/View/Form/FormContextTest.php

@@ -53,7 +53,7 @@ class FormContextTest extends TestCase
     public function testGetRequiredMessage()
     {
         $validator = new Validator();
-        $validator->notEmpty('title', 'Don\'t forget a title!');
+        $validator->notEmptyString('title', 'Don\'t forget a title!');
 
         $form = new Form();
         $form->setValidator(Form::DEFAULT_VALIDATOR, $validator);

+ 4 - 4
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5133,7 +5133,7 @@ class FormHelperTest extends TestCase
         $this->loadFixtures();
         $Articles = TableRegistry::getTableLocator()->get('Articles');
         $validator = $Articles->getValidator('default');
-        $validator->allowEmpty('user_id');
+        $validator->allowEmptyString('user_id');
         $Articles->setValidator('default', $validator);
 
         $entity = $Articles->newEmptyEntity();
@@ -7700,7 +7700,7 @@ class FormHelperTest extends TestCase
 
         $this->getTableLocator()->get('Comments')
             ->getValidator('default')
-            ->allowEmpty('comment', false);
+            ->allowEmptyString('comment', null, false);
         $result = $this->Form->control('0.comments.1.comment');
         // phpcs:disable
         $expected = [
@@ -7803,7 +7803,7 @@ class FormHelperTest extends TestCase
         $this->Form->setConfig('autoSetCustomValidity', true);
 
         $validator = (new \Cake\Validation\Validator())
-            ->notEmpty('email', 'Custom error message')
+            ->notEmptyString('email', 'Custom error message')
             ->requirePresence('password')
             ->alphaNumeric('password')
             ->notBlank('phone');
@@ -7882,7 +7882,7 @@ class FormHelperTest extends TestCase
     public function testHtml5ErrorMessageInTemplateVars()
     {
         $validator = (new \Cake\Validation\Validator())
-            ->notEmpty('email', 'Custom error "message" & entities')
+            ->notEmptyString('email', 'Custom error "message" & entities')
             ->requirePresence('password')
             ->alphaNumeric('password')
             ->notBlank('phone');