Browse Source

Fix up get() for object registry.

mscherer 6 years ago
parent
commit
2b15503bc9

+ 1 - 49
psalm-baseline.xml

@@ -32,9 +32,6 @@
     <PropertyNotSetInConstructor occurrences="1">
       <code>DigestAuthenticate</code>
     </PropertyNotSetInConstructor>
-    <TypeDoesNotContainType occurrences="1">
-      <code>empty($req)</code>
-    </TypeDoesNotContainType>
   </file>
   <file src="src/Auth/FormAuthenticate.php">
     <PossiblyInvalidArgument occurrences="2">
@@ -521,15 +518,6 @@
       <code>static::$_dsnClassMap</code>
     </UndefinedPropertyFetch>
   </file>
-  <file src="src/Core/functions.php">
-    <InvalidScalarArgument occurrences="5">
-      <code>$name</code>
-      <code>$name</code>
-      <code>$filename</code>
-      <code>env('DOCUMENT_ROOT')</code>
-      <code>env('SCRIPT_FILENAME')</code>
-    </InvalidScalarArgument>
-  </file>
   <file src="src/Database/Connection.php">
     <MissingClosureReturnType occurrences="1">
       <code>function () use ($callback) {</code>
@@ -1396,13 +1384,6 @@
     <InvalidScalarArgument occurrences="1">
       <code>$time</code>
     </InvalidScalarArgument>
-    <PossiblyInvalidArgument occurrences="5">
-      <code>env('HTTP_ACCEPT_ENCODING')</code>
-      <code>env('HTTP_ACCEPT_ENCODING')</code>
-      <code>$agent</code>
-      <code>$agent</code>
-      <code>$httpRange</code>
-    </PossiblyInvalidArgument>
     <PossiblyInvalidOperand occurrences="3">
       <code>$end</code>
       <code>$end</code>
@@ -1527,13 +1508,6 @@
     </MissingParamType>
   </file>
   <file src="src/I18n/Number.php">
-    <InvalidArgument occurrences="5">
-      <code>$size</code>
-      <code>$size / 1024</code>
-      <code>$size / 1024 / 1024</code>
-      <code>$size / 1024 / 1024 / 1024</code>
-      <code>$size / 1024 / 1024 / 1024 / 1024</code>
-    </InvalidArgument>
     <PossiblyInvalidOperand occurrences="1">
       <code>$value</code>
     </PossiblyInvalidOperand>
@@ -1573,20 +1547,6 @@
     </PossiblyUndefinedArrayOffset>
   </file>
   <file src="src/I18n/RelativeTimeFormatter.php">
-    <InvalidArgument occurrences="12">
-      <code>$count</code>
-      <code>$count</code>
-      <code>$count</code>
-      <code>$count</code>
-      <code>$count</code>
-      <code>$count</code>
-      <code>$count</code>
-      <code>$message</code>
-      <code>$message</code>
-      <code>$message</code>
-      <code>$message</code>
-      <code>'just now'</code>
-    </InvalidArgument>
     <PossiblyNullArgument occurrences="1">
       <code>$other</code>
     </PossiblyNullArgument>
@@ -1682,9 +1642,6 @@
       <code>$item</code>
       <code>$key</code>
     </MissingClosureParamType>
-    <PossiblyInvalidArgument occurrences="1">
-      <code>env('HTTP_HOST')</code>
-    </PossiblyInvalidArgument>
     <PossiblyNullArgument occurrences="1">
       <code>$this-&gt;appCharset</code>
     </PossiblyNullArgument>
@@ -1698,9 +1655,6 @@
     </PossiblyNullArgument>
   </file>
   <file src="src/Mailer/Transport/SmtpTransport.php">
-    <PossiblyInvalidArgument occurrences="1">
-      <code>$httpHost</code>
-    </PossiblyInvalidArgument>
     <PossiblyInvalidIterator occurrences="1">
       <code>$lines</code>
     </PossiblyInvalidIterator>
@@ -2800,9 +2754,7 @@
     <ImplementedReturnTypeMismatch occurrences="1">
       <code>string</code>
     </ImplementedReturnTypeMismatch>
-    <InvalidArgument occurrences="3">
-      <code>$modelName</code>
-      <code>$modelName</code>
+    <InvalidArgument occurrences="1">
       <code>$options['disabled']</code>
     </InvalidArgument>
     <PossiblyNullArgument occurrences="3">

+ 9 - 8
src/Core/ObjectRegistry.php

@@ -208,26 +208,27 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * Get loaded object instance.
      *
      * @param string $name Name of object.
-     * @return object|null Object instance if loaded else null.
+     * @return object Object instance.
+     * @throws \RuntimeException If not loaded or found.
      */
-    public function get(string $name): ?object
+    public function get(string $name): object
     {
-        if (isset($this->_loaded[$name])) {
-            return $this->_loaded[$name];
+        if (!isset($this->_loaded[$name])) {
+            throw new RuntimeException(sprintf('Unknown object "%s"', $name));
         }
 
-        return null;
+        return $this->_loaded[$name];
     }
 
     /**
      * Provide public read access to the loaded objects
      *
      * @param string $name Name of property to read
-     * @return mixed
+     * @return object|null
      */
     public function __get(string $name)
     {
-        return $this->get($name);
+        return $this->has($name) ? $this->get($name) : null;
     }
 
     /**
@@ -238,7 +239,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      */
     public function __isset(string $name): bool
     {
-        return isset($this->_loaded[$name]);
+        return $this->has($name);
     }
 
     /**

+ 4 - 3
src/ORM/Table.php

@@ -788,9 +788,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
      */
     public function getBehavior(string $name): Behavior
     {
-        /** @var \Cake\ORM\Behavior|null $behavior */
-        $behavior = $this->_behaviors->get($name);
-        if ($behavior === null) {
+        if (!$this->_behaviors->has($name)) {
             throw new InvalidArgumentException(sprintf(
                 'The %s behavior is not defined on %s.',
                 $name,
@@ -798,6 +796,9 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
             ));
         }
 
+        /** @var \Cake\ORM\Behavior|null $behavior */
+        $behavior = $this->_behaviors->get($name);
+
         return $behavior;
     }
 

+ 1 - 1
src/View/HelperRegistry.php

@@ -86,7 +86,7 @@ class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
      * Provide public read access to the loaded objects
      *
      * @param string $name Name of property to read
-     * @return mixed
+     * @return object|null
      */
     public function __get(string $name)
     {

+ 1 - 1
tests/TestCase/Database/Log/QueryLoggerTest.php

@@ -161,7 +161,7 @@ class QueryLoggerTest extends TestCase
         $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
         $query->params = ['string', '3', null];
 
-        $engine = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
+        $this->getMockBuilder('Cake\Log\Engine\BaseLog')
             ->setMethods(['log'])
             ->setConstructorArgs(['scopes' => ['queriesLog']])
             ->getMock();