Browse Source

Throw exception when creating a plugin with empty name.

ADmad 3 years ago
parent
commit
9df512c36c
2 changed files with 15 additions and 0 deletions
  1. 5 0
      src/Core/PluginCollection.php
  2. 10 0
      tests/TestCase/Core/PluginCollectionTest.php

+ 5 - 0
src/Core/PluginCollection.php

@@ -15,6 +15,7 @@ declare(strict_types=1);
  */
 namespace Cake\Core;
 
+use Cake\Core\Exception\CakeException;
 use Cake\Core\Exception\MissingPluginException;
 use Countable;
 use Generator;
@@ -232,6 +233,10 @@ class PluginCollection implements Iterator, Countable
      */
     public function create(string $name, array $config = []): PluginInterface
     {
+        if ($name === '') {
+            throw new CakeException('Cannot create a plugin with empty name');
+        }
+
         if (strpos($name, '\\') !== false) {
             /** @var \Cake\Core\PluginInterface */
             return new $name($config);

+ 10 - 0
tests/TestCase/Core/PluginCollectionTest.php

@@ -17,6 +17,7 @@ namespace Cake\Test\TestCase\Core;
 
 use Cake\Core\BasePlugin;
 use Cake\Core\Configure;
+use Cake\Core\Exception\CakeException;
 use Cake\Core\Exception\MissingPluginException;
 use Cake\Core\PluginCollection;
 use Cake\Core\PluginInterface;
@@ -129,6 +130,15 @@ class PluginCollectionTest extends TestCase
         $this->assertSame('TestTheme', $plugin->getName());
     }
 
+    public function testCreateException(): void
+    {
+        $this->expectException(CakeException::class);
+        $this->expectExceptionMessage('Cannot create a plugin with empty name');
+
+        $plugins = new PluginCollection();
+        $plugins->create('');
+    }
+
     public function testIterator(): void
     {
         $data = [