ソースを参照

Merge pull request #11737 from cakephp/3.next-class-usage

3.6: Always use consistent class usage and inclusion.
Mark Story 8 年 前
コミット
700bc5510b

+ 3 - 3
src/Database/Type.php

@@ -59,10 +59,10 @@ class Type implements TypeInterface
      * @deprecated 3.1 All types will now use a specific class
      */
     protected static $_basicTypes = [
-        'string' => ['callback' => ['\Cake\Database\Type', 'strval']],
-        'text' => ['callback' => ['\Cake\Database\Type', 'strval']],
+        'string' => ['callback' => [Type::class, 'strval']],
+        'text' => ['callback' => [Type::class, 'strval']],
         'boolean' => [
-            'callback' => ['\Cake\Database\Type', 'boolval'],
+            'callback' => [Type::class, 'boolval'],
             'pdo' => PDO::PARAM_BOOL
         ],
     ];

+ 1 - 1
src/ORM/Association/BelongsToMany.php

@@ -1430,7 +1430,7 @@ class BelongsToMany extends Association
     {
         if ($name === null) {
             if (empty($this->_junctionTableName)) {
-                $tablesNames = array_map('\Cake\Utility\Inflector::underscore', [
+                $tablesNames = array_map('Cake\Utility\Inflector::underscore', [
                     $this->getSource()->getTable(),
                     $this->getTarget()->getTable()
                 ]);

+ 1 - 1
src/ORM/Locator/LocatorInterface.php

@@ -19,7 +19,7 @@ use Cake\ORM\Table;
 /**
  * Registries for Table objects should implement this interface.
  *
- * @method array getConfig()
+ * @method array getConfig($alias)
  * @method $this setConfig($alias, $options = null)
  */
 interface LocatorInterface

+ 2 - 2
src/ORM/Table.php

@@ -763,7 +763,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     public function getEntityClass()
     {
         if (!$this->_entityClass) {
-            $default = '\Cake\ORM\Entity';
+            $default = Entity::class;
             $self = get_called_class();
             $parts = explode('\\', $self);
 
@@ -772,7 +772,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
             }
 
             $alias = Inflector::singularize(substr(array_pop($parts), 0, -5));
-            $name = implode('\\', array_slice($parts, 0, -1)) . '\Entity\\' . $alias;
+            $name = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $alias;
             if (!class_exists($name)) {
                 return $this->_entityClass = $default;
             }

+ 1 - 1
src/Template/Error/missing_action.ctp

@@ -22,7 +22,7 @@ if (!empty($plugin)) {
 }
 $prefixNs = '';
 if (!empty($prefix)) {
-    $prefix = array_map('\Cake\Utility\Inflector::camelize', explode('/', $prefix));
+    $prefix = array_map('Cake\Utility\Inflector::camelize', explode('/', $prefix));
     $prefixNs = '\\' . implode('\\', $prefix);
     $prefix = implode(DIRECTORY_SEPARATOR, $prefix) . DIRECTORY_SEPARATOR;
 }

+ 1 - 1
src/Template/Error/missing_controller.ctp

@@ -26,7 +26,7 @@ $originalClass = $class;
 $class = Inflector::camelize($class);
 
 if (!empty($prefix)) {
-    $prefix = array_map('\Cake\Utility\Inflector::camelize', explode('/', $prefix));
+    $prefix = array_map('Cake\Utility\Inflector::camelize', explode('/', $prefix));
     $prefixNs = '\\' . implode('\\', $prefix);
     $prefixPath = implode(DIRECTORY_SEPARATOR, $prefix) . DIRECTORY_SEPARATOR;
 }

+ 32 - 16
src/TestSuite/TestCase.php

@@ -17,6 +17,7 @@ use Cake\Core\App;
 use Cake\Core\Configure;
 use Cake\Datasource\ConnectionManager;
 use Cake\Event\EventManager;
+use Cake\ORM\Entity;
 use Cake\ORM\Exception\MissingTableClassException;
 use Cake\ORM\Locator\LocatorAwareTrait;
 use Cake\Routing\Router;
@@ -679,34 +680,27 @@ abstract class TestCase extends BaseTestCase
      */
     public function getMockForModel($alias, array $methods = [], array $options = [])
     {
-        $locator = $this->getTableLocator();
-
-        if (empty($options['className'])) {
-            $class = Inflector::camelize($alias);
-            $className = App::className($class, 'Model/Table', 'Table');
-            if (!$className) {
-                throw new MissingTableClassException([$alias]);
-            }
-            $options['className'] = $className;
-        }
-
-        $connectionName = $options['className']::defaultConnectionName();
+        /** @var \Cake\ORM\Table $className */
+        $className = $this->_getTableClassName($alias, $options);
+        $connectionName = $className::defaultConnectionName();
         $connection = ConnectionManager::get($connectionName);
 
+        $locator = $this->getTableLocator();
+
         list(, $baseClass) = pluginSplit($alias);
         $options += ['alias' => $baseClass, 'connection' => $connection];
         $options += $locator->getConfig($alias);
 
         /** @var \Cake\ORM\Table|\PHPUnit_Framework_MockObject_MockObject $mock */
-        $mock = $this->getMockBuilder($options['className'])
+        $mock = $this->getMockBuilder($className)
             ->setMethods($methods)
             ->setConstructorArgs([$options])
             ->getMock();
 
-        if (empty($options['entityClass']) && $mock->getEntityClass() === '\Cake\ORM\Entity') {
-            $parts = explode('\\', $options['className']);
+        if (empty($options['entityClass']) && $mock->getEntityClass() === Entity::class) {
+            $parts = explode('\\', $className);
             $entityAlias = Inflector::singularize(substr(array_pop($parts), 0, -5));
-            $entityClass = implode('\\', array_slice($parts, 0, -1)) . '\Entity\\' . $entityAlias;
+            $entityClass = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $entityAlias;
             if (class_exists($entityClass)) {
                 $mock->setEntityClass($entityClass);
             }
@@ -723,6 +717,28 @@ abstract class TestCase extends BaseTestCase
     }
 
     /**
+     * Gets the class name for the table.
+     *
+     * @param string $alias The model to get a mock for.
+     * @param array $options The config data for the mock's constructor.
+     * @return string
+     * @throws \Cake\ORM\Exception\MissingTableClassException
+     */
+    protected function _getTableClassName($alias, array $options)
+    {
+        if (empty($options['className'])) {
+            $class = Inflector::camelize($alias);
+            $className = App::className($class, 'Model/Table', 'Table');
+            if (!$className) {
+                throw new MissingTableClassException([$alias]);
+            }
+            $options['className'] = $className;
+        }
+
+        return $options['className'];
+    }
+
+    /**
      * Set the app namespace
      *
      * @param string $appNamespace The app namespace, defaults to "TestApp".

+ 3 - 3
src/Validation/RulesProvider.php

@@ -26,7 +26,7 @@ class RulesProvider
     /**
      * The class/object to proxy.
      *
-     * @var mixed
+     * @var string|object
      */
     protected $_class;
 
@@ -40,9 +40,9 @@ class RulesProvider
     /**
      * Constructor, sets the default class to use for calling methods
      *
-     * @param string $class the default class to proxy
+     * @param string|object $class the default class to proxy
      */
-    public function __construct($class = '\Cake\Validation\Validation')
+    public function __construct($class = Validation::class)
     {
         $this->_class = $class;
         $this->_reflection = new ReflectionClass($class);

+ 1 - 1
src/Validation/ValidatorAwareTrait.php

@@ -44,7 +44,7 @@ trait ValidatorAwareTrait
      *
      * @var string
      */
-    protected $_validatorClass = '\Cake\Validation\Validator';
+    protected $_validatorClass = Validator::class;
 
     /**
      * A list of validation objects indexed by name

+ 5 - 3
tests/TestCase/Event/EventDispatcherTraitTest.php

@@ -14,6 +14,8 @@
 
 namespace Cake\Test\TestCase\Event;
 
+use Cake\Event\Event;
+use Cake\Event\EventDispatcherTrait;
 use Cake\Event\EventManager;
 use Cake\TestSuite\TestCase;
 
@@ -23,7 +25,7 @@ use Cake\TestSuite\TestCase;
 class EventDispatcherTraitTest extends TestCase
 {
     /**
-     * @var EventDispatcherTrait
+     * @var \Cake\Event\EventDispatcherTrait
      */
     public $subject;
 
@@ -36,7 +38,7 @@ class EventDispatcherTraitTest extends TestCase
     {
         parent::setUp();
 
-        $this->subject = $this->getObjectForTrait('Cake\Event\EventDispatcherTrait');
+        $this->subject = $this->getObjectForTrait(EventDispatcherTrait::class);
     }
 
     /**
@@ -98,7 +100,7 @@ class EventDispatcherTraitTest extends TestCase
     {
         $event = $this->subject->dispatchEvent('some.event', ['foo' => 'bar']);
 
-        $this->assertInstanceOf('Cake\Event\Event', $event);
+        $this->assertInstanceOf(Event::class, $event);
         $this->assertSame($this->subject, $event->getSubject());
         $this->assertEquals('some.event', $event->getName());
         $this->assertEquals(['foo' => 'bar'], $event->getData());

+ 2 - 2
tests/TestCase/ORM/TableTest.php

@@ -1482,7 +1482,7 @@ class TableTest extends TestCase
     public function testEntityClassDefault()
     {
         $table = new Table();
-        $this->assertEquals('\Cake\ORM\Entity', $table->getEntityClass());
+        $this->assertEquals('Cake\ORM\Entity', $table->getEntityClass());
     }
 
     /**
@@ -5992,7 +5992,7 @@ class TableTest extends TestCase
             'registryAlias' => 'Foo.Articles',
             'table' => 'articles',
             'alias' => 'Articles',
-            'entityClass' => '\Cake\ORM\Entity',
+            'entityClass' => 'Cake\ORM\Entity',
             'associations' => [],
             'behaviors' => [],
             'defaultConnection' => 'default',

+ 2 - 2
tests/TestCase/TestSuite/TestCaseTest.php

@@ -409,7 +409,7 @@ class TestCaseTest extends TestCase
             ->method('save')
             ->will($this->returnValue('mocked'));
         $this->assertEquals('mocked', $Posts->save($entity));
-        $this->assertEquals('\Cake\ORM\Entity', $Posts->getEntityClass());
+        $this->assertEquals('Cake\ORM\Entity', $Posts->getEntityClass());
 
         $Posts = $this->getMockForModel('Posts', ['doSomething']);
         $this->assertInstanceOf('Cake\Database\Connection', $Posts->getConnection());
@@ -450,7 +450,7 @@ class TestCaseTest extends TestCase
         $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', ['save']);
 
         $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
-        $this->assertEquals('\Cake\ORM\Entity', $TestPluginComment->getEntityClass());
+        $this->assertEquals('Cake\ORM\Entity', $TestPluginComment->getEntityClass());
         $TestPluginComment->expects($this->at(0))
             ->method('save')
             ->will($this->returnValue(true));