Browse Source

Fix up phpunit, formatting and static analyzers

Mark Story 2 years ago
parent
commit
d9714ac85d

+ 44 - 24
phpstan-baseline.neon

@@ -1,6 +1,11 @@
 parameters:
 	ignoreErrors:
 		-
+			message: "#^Parameter \\#1 \\$iterator of class LimitIterator constructor expects TIterator of Iterator\\<mixed, mixed\\>, \\$this\\(Cake\\\\Collection\\\\Collection\\) given\\.$#"
+			count: 3
+			path: src/Collection/Collection.php
+
+		-
 			message: "#^Unsafe usage of new static\\(\\)\\.$#"
 			count: 1
 			path: src/Collection/Iterator/NestIterator.php
@@ -11,49 +16,44 @@ parameters:
 			path: src/Collection/Iterator/NoChildrenIterator.php
 
 		-
-			message: "#^Unsafe usage of new static\\(\\)\\.$#"
-			count: 2
-			path: src/Console/ConsoleOptionParser.php
+			message: "#^Parameter \\#1 \\$iterator of class LimitIterator constructor expects TIterator of Iterator\\<mixed, mixed\\>, \\$this\\(Cake\\\\Collection\\\\Iterator\\\\TreeIterator\\) given\\.$#"
+			count: 3
+			path: src/Collection/Iterator/TreeIterator.php
 
 		-
-			message: "#^Dead catch \\- Cake\\\\Console\\\\Exception\\\\StopException is never thrown in the try block\\.$#"
-			count: 1
-			path: src/Console/ShellDispatcher.php
+			message: "#^Parameter \\#1 \\$iterator of class LimitIterator constructor expects TIterator of Iterator\\<mixed, mixed\\>, \\$this\\(Cake\\\\Collection\\\\Iterator\\\\TreePrinter\\) given\\.$#"
+			count: 3
+			path: src/Collection/Iterator/TreePrinter.php
 
 		-
-			message: "#^Property Cake\\\\Controller\\\\Controller\\:\\:\\$request \\(Cake\\\\Http\\\\ServerRequest\\) on left side of \\?\\? is not nullable\\.$#"
-			count: 1
-			path: src/Controller/Controller.php
+			message: "#^Parameter \\#1 \\$iterator of class LimitIterator constructor expects TIterator of Iterator\\<mixed, mixed\\>, \\$this\\(Cake\\\\Collection\\\\Iterator\\\\ZipIterator\\) given\\.$#"
+			count: 3
+			path: src/Collection/Iterator/ZipIterator.php
 
 		-
-			message: "#^Property Cake\\\\Controller\\\\Controller\\:\\:\\$response \\(Cake\\\\Http\\\\Response\\) does not accept Psr\\\\Http\\\\Message\\\\ResponseInterface\\.$#"
+			message: "#^Call to method getOptionParser\\(\\) on an unknown class Cake\\\\Console\\\\Shell\\.$#"
 			count: 1
-			path: src/Controller/Controller.php
+			path: src/Command/CompletionCommand.php
 
 		-
-			message: "#^Property Cake\\\\Controller\\\\Controller\\:\\:\\$response \\(Cake\\\\Http\\\\Response\\) on left side of \\?\\? is not nullable\\.$#"
+			message: "#^Method Cake\\\\Console\\\\CommandCollection\\:\\:get\\(\\) has invalid return type Cake\\\\Console\\\\Shell\\.$#"
 			count: 1
-			path: src/Controller/Controller.php
+			path: src/Console/CommandCollection.php
 
 		-
-			message: "#^Parameter \\#1 \\$request of method Cake\\\\Controller\\\\Controller\\:\\:setRequest\\(\\) expects Cake\\\\Http\\\\ServerRequest, Psr\\\\Http\\\\Message\\\\ServerRequestInterface given\\.$#"
+			message: "#^Method Cake\\\\Console\\\\CommandCollection\\:\\:getIterator\\(\\) has invalid return type Cake\\\\Console\\\\Shell\\.$#"
 			count: 1
-			path: src/Controller/ControllerFactory.php
+			path: src/Console/CommandCollection.php
 
 		-
-			message: "#^Parameter \\#1 \\$request of method Cake\\\\Controller\\\\ControllerFactory\\:\\:getControllerClass\\(\\) expects Cake\\\\Http\\\\ServerRequest, Psr\\\\Http\\\\Message\\\\ServerRequestInterface given\\.$#"
+			message: "#^PHPDoc tag @return with type Cake\\\\Console\\\\CommandInterface\\|Cake\\\\Console\\\\Shell\\|class\\-string\\<Cake\\\\Console\\\\CommandInterface\\> is not subtype of native type Cake\\\\Console\\\\CommandInterface\\|string\\.$#"
 			count: 1
-			path: src/Controller/ControllerFactory.php
-
-		-
-			message: "#^Parameter \\#1 \\$request of method Cake\\\\Controller\\\\ControllerFactory\\:\\:missingController\\(\\) expects Cake\\\\Http\\\\ServerRequest, Psr\\\\Http\\\\Message\\\\ServerRequestInterface given\\.$#"
-			count: 2
-			path: src/Controller/ControllerFactory.php
+			path: src/Console/CommandCollection.php
 
 		-
-			message: "#^Property Cake\\\\Database\\\\Driver\\:\\:\\$_connection \\(PDO\\) does not accept null\\.$#"
+			message: "#^Unsafe usage of new static\\(\\)\\.$#"
 			count: 2
-			path: src/Database/Driver.php
+			path: src/Console/ConsoleOptionParser.php
 
 		-
 			message: "#^Unsafe usage of new static\\(\\)\\.$#"
@@ -141,11 +141,31 @@ parameters:
 			path: src/ORM/Query/SelectQuery.php
 
 		-
+			message: "#^Parameter \\#1 \\$iterator of class LimitIterator constructor expects TIterator of Iterator\\<mixed, mixed\\>, \\$this\\(Cake\\\\ORM\\\\ResultSet\\<T of array\\|Cake\\\\Datasource\\\\EntityInterface\\>\\) given\\.$#"
+			count: 2
+			path: src/ORM/ResultSet.php
+
+		-
 			message: "#^Unsafe usage of new static\\(\\)\\.$#"
 			count: 1
 			path: src/Routing/RouteBuilder.php
 
 		-
+			message: "#^Parameter \\#5 \\$trace of class Cake\\\\Error\\\\PhpError constructor expects array, array\\|string given\\.$#"
+			count: 1
+			path: src/TestSuite/TestCase.php
+
+		-
+			message: "#^Unreachable statement \\- code above always terminates\\.$#"
+			count: 1
+			path: src/TestSuite/TestCase.php
+
+		-
+			message: "#^Unreachable statement \\- code above always terminates\\.$#"
+			count: 1
+			path: src/Utility/Hash.php
+
+		-
 			message: "#^Unsafe usage of new static\\(\\)\\.$#"
 			count: 1
 			path: src/View/Form/ContextFactory.php

+ 106 - 69
psalm-baseline.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<files psalm-version="5.9.0@8b9ad1eb9e8b7d3101f949291da2b9f7767cd163">
+<files psalm-version="5.7.4@c46eccda769925073b8f65d66c4a3a7dc5d440b1">
   <file src="src/Cache/Engine/FileEngine.php">
     <TooManyTemplateParams>
       <code>$iterator</code>
@@ -17,103 +17,125 @@
       <code>int|false</code>
       <code>int|false</code>
     </InvalidReturnType>
-  </file>
-  <file src="src/Collection/CollectionTrait.php">
-    <InvalidArrayOffset>
-      <code>$collectionArraysCounts[$changeIndex]</code>
-      <code>$value[$keys[$index]]</code>
-    </InvalidArrayOffset>
-  </file>
-  <file src="src/Collection/Iterator/ZipIterator.php">
-    <MissingTemplateParam>
-      <code>ZipIterator</code>
-    </MissingTemplateParam>
-  </file>
-  <file src="src/Controller/Component/RequestHandlerComponent.php">
-    <RedundantCondition>
-      <code>is_string($type)</code>
-    </RedundantCondition>
-  </file>
-  <file src="src/Database/Expression/QueryExpression.php">
-    <RedundantCondition>
-      <code>$typeMultiple</code>
-    </RedundantCondition>
+    <UndefinedConstant>
+      <code>Redis::OPT_SCAN</code>
+      <code>Redis::OPT_SCAN</code>
+      <code>Redis::SCAN_RETRY</code>
+      <code>Redis::SCAN_RETRY</code>
+    </UndefinedConstant>
+  </file>
+  <file src="src/Console/CommandCollection.php">
+    <InvalidDocblock>
+      <code>public function get(string $name): CommandInterface|string</code>
+      <code>public function getIterator(): Traversable</code>
+    </InvalidDocblock>
   </file>
   <file src="src/Database/Query.php">
-    <MissingTemplateParam>
-      <code>IteratorAggregate</code>
-    </MissingTemplateParam>
-  </file>
-  <file src="src/Database/Statement/PDOStatement.php">
-    <RedundantCondition>
-      <code><![CDATA[$property === 'queryString' && isset($this->_statement->queryString)]]></code>
-    </RedundantCondition>
-  </file>
-  <file src="src/Error/Renderer/ConsoleExceptionRenderer.php">
-    <InvalidArrayOffset>
-      <code>$exceptions[$i - 1]</code>
-    </InvalidArrayOffset>
-  </file>
-  <file src="src/I18n/DateFormatTrait.php">
-    <RedundantCondition>
-      <code>$time !== false</code>
-    </RedundantCondition>
-  </file>
-  <file src="src/ORM/SaveOptionsBuilder.php">
-    <MissingTemplateParam>
-      <code>SaveOptionsBuilder</code>
-    </MissingTemplateParam>
+    <UnusedPsalmSuppress>
+      <code>InvalidArrayOffset</code>
+    </UnusedPsalmSuppress>
+  </file>
+  <file src="src/Datasource/Paging/PaginatedResultSet.php">
+    <MethodSignatureMustProvideReturnType>
+      <code>://cakephp.org)</code>
+    </MethodSignatureMustProvideReturnType>
+  </file>
+  <file src="src/Event/EventDispatcherTrait.php">
+    <MoreSpecificImplementedParamType>
+      <code>$subject</code>
+    </MoreSpecificImplementedParamType>
+  </file>
+  <file src="src/Event/EventManager.php">
+    <InvalidArgument>
+      <code>_callListener</code>
+      <code>addEventToList</code>
+      <code>addEventToList</code>
+    </InvalidArgument>
+    <InvalidReturnStatement>
+      <code>$event</code>
+      <code>$event</code>
+    </InvalidReturnStatement>
+    <InvalidReturnType>
+      <code>EventInterface</code>
+    </InvalidReturnType>
   </file>
-  <file src="src/Shell/Helper/TableHelper.php">
-    <InvalidCast>
-      <code>$v</code>
-    </InvalidCast>
+  <file src="src/Http/ServerRequest.php">
+    <MoreSpecificImplementedParamType>
+      <code>$requestTarget</code>
+    </MoreSpecificImplementedParamType>
+  </file>
+  <file src="src/I18n/Date.php">
+    <ImpureFunctionCall>
+      <code>call_user_func(static::$_jsonEncodeFormat, $this)</code>
+      <code>static::$_jsonEncodeFormat</code>
+    </ImpureFunctionCall>
+    <ImpureMethodCall>
+      <code>_formatObject</code>
+      <code>dateAgoInWords</code>
+      <code>diffFormatter</code>
+      <code>getDefaultLocale</code>
+    </ImpureMethodCall>
+    <ImpureStaticProperty>
+      <code>static::$_jsonEncodeFormat</code>
+      <code>static::$_jsonEncodeFormat</code>
+      <code>static::$_jsonEncodeFormat</code>
+      <code>static::$_toStringFormat</code>
+      <code>static::$niceFormat</code>
+    </ImpureStaticProperty>
+  </file>
+  <file src="src/I18n/DateTime.php">
+    <ImpureFunctionCall>
+      <code>call_user_func(static::$_jsonEncodeFormat, $this)</code>
+      <code>static::$_jsonEncodeFormat</code>
+    </ImpureFunctionCall>
+    <ImpureMethodCall>
+      <code>_formatObject</code>
+      <code>diffFormatter</code>
+      <code>getDefaultLocale</code>
+      <code>timeAgoInWords</code>
+    </ImpureMethodCall>
+    <ImpureStaticProperty>
+      <code>static::$_jsonEncodeFormat</code>
+      <code>static::$_jsonEncodeFormat</code>
+      <code>static::$_jsonEncodeFormat</code>
+      <code>static::$_toStringFormat</code>
+      <code>static::$niceFormat</code>
+    </ImpureStaticProperty>
+  </file>
+  <file src="src/ORM/Table.php">
+    <InvalidReturnStatement>
+      <code><![CDATA[$this->_behaviors->callFinder($type, [$query, $options])]]></code>
+    </InvalidReturnStatement>
   </file>
   <file src="src/TestSuite/Constraint/EventFired.php">
     <InternalClass>
-      <code>new AssertionFailedError(
+      <code><![CDATA[new AssertionFailedError(
                 'The event manager you are asserting against is not configured to track events.'
-            )</code>
+            )]]></code>
     </InternalClass>
     <InternalMethod>
-      <code>new AssertionFailedError(
+      <code><![CDATA[new AssertionFailedError(
                 'The event manager you are asserting against is not configured to track events.'
-            )</code>
+            )]]></code>
     </InternalMethod>
   </file>
   <file src="src/TestSuite/Constraint/EventFiredWith.php">
     <InternalClass>
-<<<<<<< HEAD
       <code><![CDATA[new AssertionFailedError(
                 'The event manager you are asserting against is not configured to track events.'
             )]]></code>
       <code><![CDATA[new AssertionFailedError(sprintf(
                 'Event `%s` was fired %d times, cannot make data assertion',
-=======
-      <code>new AssertionFailedError(
-                'The event manager you are asserting against is not configured to track events.'
-            )</code>
-      <code><![CDATA[new AssertionFailedError(sprintf(
-                'Event "%s" was fired %d times, cannot make data assertion',
->>>>>>> 4.next
                 $other,
                 count($events)
             ))]]></code>
     </InternalClass>
     <InternalMethod>
-<<<<<<< HEAD
       <code><![CDATA[new AssertionFailedError(
                 'The event manager you are asserting against is not configured to track events.'
             )]]></code>
       <code><![CDATA[new AssertionFailedError(sprintf(
                 'Event `%s` was fired %d times, cannot make data assertion',
-=======
-      <code>new AssertionFailedError(
-                'The event manager you are asserting against is not configured to track events.'
-            )</code>
-      <code><![CDATA[new AssertionFailedError(sprintf(
-                'Event "%s" was fired %d times, cannot make data assertion',
->>>>>>> 4.next
                 $other,
                 count($events)
             ))]]></code>
@@ -146,4 +168,19 @@
       <code>is_array($_list)</code>
     </RedundantCondition>
   </file>
+  <file src="src/View/Exception/MissingCellTemplateException.php">
+    <ImplementedReturnTypeMismatch>
+      <code><![CDATA[array{name: string, file: string, paths: array<string>}]]></code>
+    </ImplementedReturnTypeMismatch>
+  </file>
+  <file src="src/View/HelperRegistry.php">
+    <UnusedPsalmSuppress>
+      <code>NoValue</code>
+    </UnusedPsalmSuppress>
+  </file>
+  <file src="src/View/Widget/RadioWidget.php">
+    <UnusedPsalmSuppress>
+      <code>TypeDoesNotContainType</code>
+    </UnusedPsalmSuppress>
+  </file>
 </files>

+ 1 - 6
psalm.xml

@@ -1,11 +1,6 @@
 <?xml version="1.0"?>
 <psalm
     errorLevel="4"
-    allowStringToStandInForClass="true"
-    findUnusedPsalmSuppress="true"
-    findUnusedBaselineEntry="true"
-    findUnusedCode="false"
-    resolveFromConfigFile="true"
     usePhpDocMethodsWithoutMagicCall="true"
     findUnusedPsalmSuppress="true"
     findUnusedBaselineEntry="true"
@@ -15,7 +10,7 @@
     xmlns="https://getpsalm.org/schema/config"
     xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
     autoloader="tests/bootstrap.php"
-    errorBaseline="psalm-baseline.xml"
+    errorBaseline="./psalm-baseline.xml"
 >
     <projectFiles>
         <directory name="src" />

+ 2 - 4
src/Database/Query.php

@@ -25,6 +25,7 @@ use Cake\Database\Expression\QueryExpression;
 use Closure;
 use InvalidArgumentException;
 use Stringable;
+use Throwable;
 
 /**
  * This class represents a Relational database SQL Query. A query can be of
@@ -744,7 +745,7 @@ abstract class Query implements ExpressionInterface, Stringable
         }
 
         /**
-         * @var string $alias */
+         * @var string $alias
          * @psalm-suppress InvalidArrayOffset
          */
         return [
@@ -1836,12 +1837,9 @@ abstract class Query implements ExpressionInterface, Stringable
             );
             $sql = $this->sql();
             $params = $this->getValueBinder()->bindings();
-<<<<<<< HEAD
-=======
         } catch (Throwable $e) {
             $sql = 'SQL could not be generated for this query as it is incomplete.';
             $params = [];
->>>>>>> 4.next
         } finally {
             restore_error_handler();
 

+ 1 - 5
src/Routing/RouteCollection.php

@@ -61,11 +61,7 @@ class RouteCollection
      *
      * @var array<string, array<\Cake\Routing\Route\Route>>
      */
-<<<<<<< HEAD
-    protected array $dynamicPaths = [];
-=======
-    protected $_paths = [];
->>>>>>> 4.next
+    protected array $_paths = [];
 
     /**
      * A map of middleware names and the related objects.

+ 1 - 1
src/TestSuite/TestCase.php

@@ -75,7 +75,7 @@ abstract class TestCase extends BaseTestCase
     /**
      * @var \Cake\Error\PhpError|null
      */
-    private $_capturedError;
+    private ?PhpError $_capturedError = null;
 
     /**
      * Asserts that a string matches a given regular expression.

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

@@ -126,7 +126,7 @@ class RadioWidget extends BasicWidget
      * Disabled attribute detection.
      *
      * @param array<string, mixed> $radio Radio info.
-     * @param array|true|string|null $disabled The disabled values.
+     * @param array|string|true|null $disabled The disabled values.
      * @return bool
      */
     protected function _isDisabled(array $radio, array|string|bool|null $disabled): bool

+ 6 - 15
tests/TestCase/Core/FunctionsGlobalTest.php

@@ -20,6 +20,7 @@ use Cake\Core\Configure;
 use Cake\Http\Response;
 use Cake\TestSuite\TestCase;
 use stdClass;
+use function env;
 
 require_once CAKE . 'Core/functions_global.php';
 
@@ -34,7 +35,7 @@ class FunctionsGlobalTest extends TestCase
     public function testEnv(): void
     {
         $_ENV['DOES_NOT_EXIST'] = null;
-        $this->assertNull(\env('DOES_NOT_EXIST'));
+        $this->assertNull(env('DOES_NOT_EXIST'));
         $this->assertSame('default', env('DOES_NOT_EXIST', 'default'));
 
         $_ENV['DOES_EXIST'] = 'some value';
@@ -268,7 +269,7 @@ class FunctionsGlobalTest extends TestCase
     public function testDeprecationWarningEnabled(): void
     {
         $error = $this->captureError(E_ALL, function (): void {
-            deprecationWarning('This is deprecated ' . uniqid(), 2);
+            deprecationWarning('4.5.0', 'This is deprecated ' . uniqid(), 2);
         });
         $this->assertMatchesRegularExpression(
             '/This is deprecated \w+\n(.*?)[\/\\\]FunctionsGlobalTest.php, line\: \d+/',
@@ -282,7 +283,7 @@ class FunctionsGlobalTest extends TestCase
     public function testDeprecationWarningEnabledDefaultFrame(): void
     {
         $error = $this->captureError(E_ALL, function (): void {
-            deprecationWarning('This is going away too ' . uniqid());
+            deprecationWarning('5.0.0', 'This is going away too ' . uniqid());
         });
         $this->assertMatchesRegularExpression(
             '/This is going away too \w+\n(.*?)[\/\\\]TestCase.php, line\: \d+/',
@@ -299,7 +300,7 @@ class FunctionsGlobalTest extends TestCase
 
         Configure::write('Error.ignoredDeprecationPaths', ['src/TestSuite/*']);
         $this->withErrorReporting(E_ALL, function (): void {
-            deprecationWarning('This will be gone soon');
+            deprecationWarning('5.0.1', 'This will be gone soon');
         });
     }
 
@@ -311,7 +312,7 @@ class FunctionsGlobalTest extends TestCase
         $this->expectNotToPerformAssertions();
 
         $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function (): void {
-            deprecationWarning('This is leaving');
+            deprecationWarning('5.0.0', 'This is leaving');
         });
     }
 
@@ -337,14 +338,4 @@ class FunctionsGlobalTest extends TestCase
             $this->assertTrue(true);
         });
     }
-
-    /**
-     * testing getTypeName()
-     */
-    public function testgetTypeName(): void
-    {
-        $this->assertSame('stdClass', getTypeName(new \stdClass()));
-        $this->assertSame('array', getTypeName([]));
-        $this->assertSame('string', getTypeName(''));
-    }
 }

+ 2 - 3
tests/TestCase/Core/FunctionsTest.php

@@ -19,7 +19,6 @@ namespace Cake\Test\TestCase\Core;
 use Cake\Core\Configure;
 use Cake\Http\Response;
 use Cake\TestSuite\TestCase;
-use Exception;
 use stdClass;
 use function Cake\Core\deprecationWarning;
 use function Cake\Core\env;
@@ -281,7 +280,7 @@ class FunctionsTest extends TestCase
     public function testDeprecationWarningEnabled(): void
     {
         $error = $this->captureError(E_ALL, function (): void {
-            deprecationWarning('This is going away', 2);
+            deprecationWarning('5.0.0', 'This is going away', 2);
         });
         $this->assertMatchesRegularExpression(
             '/This is going away\n(.*?)[\/\\\]FunctionsTest.php, line\: \d+/',
@@ -295,7 +294,7 @@ class FunctionsTest extends TestCase
     public function testDeprecationWarningEnabledDefaultFrame(): void
     {
         $error = $this->captureError(E_ALL, function (): void {
-            deprecationWarning('This is going away too');
+            deprecationWarning('5.0.0', 'This is going away too');
         });
         $this->assertMatchesRegularExpression(
             '/This is going away too\n(.*?)[\/\\\]TestCase.php, line\: \d+/',