Ver código fonte

Raise phpstan level to 1 (#8027)

Adam Žurek 5 anos atrás
pai
commit
25cd2382cb

+ 19 - 1
phpstan/config.neon

@@ -1,14 +1,32 @@
 parameters:
+    level: 1
     autoload_files:
         - autoload.php
-    level: 0
     excludes_analyse:
        - '../tests/Composer/Test/Fixtures/*'
        - '../tests/Composer/Test/Autoload/Fixtures/*'
        - '../tests/Composer/Test/Plugin/Fixtures/*'
     ignoreErrors:
+        # unused parameters
+        - '~^Constructor of class Composer\\Repository\\VcsRepository has an unused parameter \$dispatcher\.$~'
+        - '~^Constructor of class Composer\\Repository\\PearRepository has an unused parameter \$dispatcher\.$~'
+        - '~^Constructor of class Composer\\Util\\Http\\CurlDownloader has an unused parameter \$disableTls\.$~'
+        - '~^Constructor of class Composer\\Util\\Http\\CurlDownloader has an unused parameter \$options\.$~'
+        - '~^Constructor of class Composer\\Repository\\PearRepository has an unused parameter \$config\.$~'
+
         # ion cube is not installed
         - '~^Function ioncube_loader_\w+ not found\.$~'
+        # rar is not installed
+        - '~^Call to static method open\(\) on an unknown class RarArchive\.$~'
+        # imagick is not installed
+        - '~^Instantiated class Imagick not found\.$~'
+        # windows specific constants
+        -
+            message: '~^Constant PHP_WINDOWS_VERSION_MAJOR not found\.$~'
+            path: '*/src/Composer/Downloader/PathDownloader.php'
+        -
+            message: '~^Constant PHP_WINDOWS_VERSION_MINOR not found\.$~'
+            path: '*/src/Composer/Downloader/PathDownloader.php'
 
         # variables from global scope
         - '~^Undefined variable: \$vendorDir$~'

+ 6 - 0
src/Composer/Command/DiagnoseCommand.php

@@ -634,6 +634,9 @@ EOT
                         $text = PHP_EOL."The openssl extension is missing, which means that secure HTTPS transfers are impossible.".PHP_EOL;
                         $text .= "If possible you should enable it or recompile php with --with-openssl";
                         break;
+
+                    default:
+                        throw new \InvalidArgumentException(sprintf("DiagnoseCommand: Unknown error type \"%s\". Please report at https://github.com/composer/composer/issues/new.", $error));
                 }
                 $out($text, 'error');
             }
@@ -698,6 +701,9 @@ EOT
                         $text = "The Windows OneDrive folder is not supported on PHP versions below 7.2.23 and 7.3.10.".PHP_EOL;
                         $text .= "Upgrade your PHP ({$current}) to use this location with Composer.".PHP_EOL;
                         break;
+
+                    default:
+                        throw new \InvalidArgumentException(sprintf("DiagnoseCommand: Unknown warning type \"%s\". Please report at https://github.com/composer/composer/issues/new.", $warning));
                 }
                 $out($text, 'comment');
             }

+ 1 - 1
src/Composer/DependencyResolver/DefaultPolicy.php

@@ -47,9 +47,9 @@ class DefaultPolicy implements PolicyInterface
     public function selectPreferredPackages(Pool $pool, array $literals, $requiredPackage = null)
     {
         $packages = $this->groupLiteralsByName($pool, $literals);
+        $policy = $this;
 
         foreach ($packages as &$nameLiterals) {
-            $policy = $this;
             usort($nameLiterals, function ($a, $b) use ($policy, $pool, $requiredPackage) {
                 return $policy->compareByPriority($pool, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage, true);
             });

+ 1 - 0
src/Composer/Downloader/FileDownloader.php

@@ -362,6 +362,7 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface
         $this->io = new NullIO;
         $this->io->loadConfiguration($this->config);
         $e = null;
+        $output = '';
 
         try {
             $res = $this->download($package, $targetDir.'_compare', null, false);

+ 4 - 0
src/Composer/Json/JsonManipulator.php

@@ -356,6 +356,10 @@ class JsonManipulator
             $childrenClean = $children;
         }
 
+        if (!isset($childrenClean)) {
+            throw new \InvalidArgumentException("JsonManipulator: \$childrenClean is not defined. Please report at https://github.com/composer/composer/issues/new.");
+        }
+
         // no child data left, $name was the only key in
         $this->pregMatch('#^{ \s*? (?P<content>\S+.*?)? (?P<trailingspace>\s*) }$#sx', $childrenClean, $match);
         if (empty($match['content'])) {

+ 5 - 0
src/Composer/Package/Loader/JsonLoader.php

@@ -38,6 +38,11 @@ class JsonLoader
             $config = JsonFile::parseJson(file_get_contents($json), $json);
         } elseif (is_string($json)) {
             $config = JsonFile::parseJson($json);
+        } else {
+            throw new \InvalidArgumentException(sprintf(
+                "JsonLoader: Unknown \$json parameter %s. Please report at https://github.com/composer/composer/issues/new.",
+                gettype($json)
+            ));
         }
 
         return $this->loader->load($config);

+ 2 - 0
src/Composer/Package/Package.php

@@ -616,6 +616,8 @@ class Package extends BasePackage
                     $mirrorUrl = ComposerMirror::processGitUrl($mirror['url'], $this->name, $url, $type);
                 } elseif ($urlType === 'source' && $type === 'hg') {
                     $mirrorUrl = ComposerMirror::processHgUrl($mirror['url'], $this->name, $url, $type);
+                } else {
+                    continue;
                 }
                 if (!in_array($mirrorUrl, $urls)) {
                     $func = $mirror['preferred'] ? 'array_unshift' : 'array_push';

+ 4 - 0
src/Composer/Repository/ComposerRepository.php

@@ -1078,6 +1078,10 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
             }
         }
 
+        if (!isset($data)) {
+            throw new \LogicException("ComposerRepository: Undefined \$data. Please report at https://github.com/composer/composer/issues/new.");
+        }
+
         return $data;
     }
 

+ 6 - 6
src/Composer/Repository/Vcs/GitLabDriver.php

@@ -366,13 +366,13 @@ class GitLabDriver extends VcsDriver
 
     protected function attemptCloneFallback()
     {
-        try {
-            if ($this->isPrivate === false) {
-                $url = $this->generatePublicUrl();
-            } else {
-                $url = $this->generateSshUrl();
-            }
+        if ($this->isPrivate === false) {
+            $url = $this->generatePublicUrl();
+        } else {
+            $url = $this->generateSshUrl();
+        }
 
+        try {
             // If this repository may be private and we
             // cannot ask for authentication credentials (because we
             // are not interactive) then we fallback to GitDriver.

+ 3 - 2
src/Composer/Util/AuthHelper.php

@@ -161,9 +161,10 @@ class AuthHelper
             if (!$this->io->isInteractive()) {
                 if ($statusCode === 401) {
                     $message = "The '" . $url . "' URL required authentication.\nYou must be using the interactive console to authenticate";
-                }
-                if ($statusCode === 403) {
+                } elseif ($statusCode === 403) {
                     $message = "The '" . $url . "' URL could not be accessed: " . $reason;
+                } else {
+                    $message = "Unknown error code '" . $statusCode . "', reason: " . $reason;
                 }
 
                 throw new TransportException($message, $statusCode);

+ 1 - 1
src/Composer/Util/ConfigValidator.php

@@ -171,8 +171,8 @@ class ConfigValidator
             $warnings[] = "Defining autoload.psr-4 with an empty namespace prefix is a bad idea for performance";
         }
 
+        $loader = new ValidatingArrayLoader(new ArrayLoader(), true, null, $arrayLoaderValidationFlags);
         try {
-            $loader = new ValidatingArrayLoader(new ArrayLoader(), true, null, $arrayLoaderValidationFlags);
             if (!isset($manifest['version'])) {
                 $manifest['version'] = '1.0.0';
             }

+ 2 - 2
src/Composer/Util/Git.php

@@ -79,7 +79,7 @@ class Git
                     return;
                 }
                 $messages[] = '- ' . $protoUrl . "\n" . preg_replace('#^#m', '  ', $this->process->getErrorOutput());
-                if ($initialClone) {
+                if ($initialClone && isset($origCwd)) {
                     $this->filesystem->removeDirectory($origCwd);
                 }
             }
@@ -238,7 +238,7 @@ class Git
                 }
             }
 
-            if ($initialClone) {
+            if ($initialClone && isset($origCwd)) {
                 $this->filesystem->removeDirectory($origCwd);
             }
 

+ 3 - 0
src/Composer/Util/RemoteFilesystem.php

@@ -269,6 +269,7 @@ class RemoteFilesystem
 
             return true;
         });
+        $http_response_header = array();
         try {
             $result = $this->getRemoteContents($originUrl, $fileUrl, $ctx, $http_response_header);
 
@@ -537,6 +538,8 @@ class RemoteFilesystem
      */
     protected function getRemoteContents($originUrl, $fileUrl, $context, array &$responseHeaders = null)
     {
+        $result = false;
+
         try {
             $e = null;
             $result = file_get_contents($fileUrl, false, $context);

+ 2 - 1
tests/Composer/Test/InstallerTest.php

@@ -319,7 +319,7 @@ class InstallerTest extends TestCase
 
         $output = str_replace("\r", '', $io->getOutput());
         $this->assertEquals($expectResult, $result, $output . stream_get_contents($appOutput));
-        if ($expectLock) {
+        if ($expectLock && isset($actualLock)) {
             unset($actualLock['hash']);
             unset($actualLock['content-hash']);
             unset($actualLock['_readme']);
@@ -434,6 +434,7 @@ class InstallerTest extends TestCase
         );
 
         $section = null;
+        $data = array();
         foreach ($tokens as $i => $token) {
             if (null === $section && empty($token)) {
                 continue; // skip leading blank