1
0
Fork 0

Add types to `Package/Loader` (#10206)

pull/10211/head
Martin Herndl 2021-10-25 13:08:05 +02:00 committed by GitHub
parent b1a96a3141
commit 13d433fad0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 136 additions and 30 deletions

View File

@ -23,7 +23,7 @@ use Composer\Repository\PlatformRepository;
abstract class BasePackage implements PackageInterface
{
/**
* @phpstan-var array<string, array{description: string, method: Link::TYPE_*}>
* @phpstan-var array<non-empty-string, array{description: string, method: Link::TYPE_*}>
* @internal
*/
public static $supportedLinkTypes = array(

View File

@ -12,7 +12,6 @@
namespace Composer\Package\Loader;
use Composer\Package\AliasPackage;
use Composer\Package\BasePackage;
use Composer\Package\CompleteAliasPackage;
use Composer\Package\CompletePackage;
@ -20,9 +19,7 @@ use Composer\Package\RootPackage;
use Composer\Package\PackageInterface;
use Composer\Package\CompletePackageInterface;
use Composer\Package\Link;
use Composer\Package\Package;
use Composer\Package\RootAliasPackage;
use Composer\Package\RootPackageInterface;
use Composer\Package\Version\VersionParser;
/**
@ -36,6 +33,9 @@ class ArrayLoader implements LoaderInterface
/** @var bool */
protected $loadOptions;
/**
* @param bool $loadOptions
*/
public function __construct(VersionParser $parser = null, $loadOptions = false)
{
if (!$parser) {
@ -46,14 +46,7 @@ class ArrayLoader implements LoaderInterface
}
/**
* @template PackageClass of CompletePackageInterface
*
* @param array $config package data
* @param string $class FQCN to be instantiated
*
* @return CompletePackage|CompleteAliasPackage|RootPackage|RootAliasPackage
*
* @phpstan-param class-string<PackageClass> $class
* @inheritDoc
*/
public function load(array $config, $class = 'Composer\Package\CompletePackage')
{
@ -83,7 +76,8 @@ class ArrayLoader implements LoaderInterface
}
/**
* @param array $versions
* @param list<array<mixed>> $versions
*
* @return list<CompletePackage|CompleteAliasPackage>
*/
public function loadPackages(array $versions)
@ -105,9 +99,12 @@ class ArrayLoader implements LoaderInterface
/**
* @template PackageClass of CompletePackageInterface
* @param array $config package data
* @param string $class FQCN to be instantiated
*
* @param mixed[] $config package data
* @param string $class FQCN to be instantiated
*
* @return CompletePackage|RootPackage
*
* @phpstan-param class-string<PackageClass> $class
*/
private function createObject(array $config, $class)
@ -135,8 +132,9 @@ class ArrayLoader implements LoaderInterface
}
/**
* @param CompletePackage $package
* @param array $config package data
* @param CompletePackage $package
* @param mixed[] $config package data
*
* @return RootPackage|RootAliasPackage|CompletePackage|CompleteAliasPackage
*/
private function configureObject(PackageInterface $package, array $config)
@ -311,9 +309,10 @@ class ArrayLoader implements LoaderInterface
}
/**
* @param array $linkCache
* @param PackageInterface $package
* @param array $config
* @param array<string, array<string, array<string, array<string, array{string, Link}>>>> $linkCache
* @param PackageInterface $package
* @param mixed[] $config
*
* @return void
*/
private function configureCachedLinks(&$linkCache, $package, array $config)
@ -346,11 +345,14 @@ class ArrayLoader implements LoaderInterface
}
/**
* @param string $source source package name
* @param string $sourceVersion source package version (pretty version ideally)
* @param Link::TYPE_* $description link description (e.g. requires, replaces, ..)
* @param array $links array of package name => constraint mappings
* @param string $source source package name
* @param string $sourceVersion source package version (pretty version ideally)
* @param string $description link description (e.g. requires, replaces, ..)
* @param array<string, string> $links array of package name => constraint mappings
*
* @return Link[]
*
* @phpstan-param Link::TYPE_* $description
*/
public function parseLinks($source, $sourceVersion, $description, $links)
{
@ -387,7 +389,8 @@ class ArrayLoader implements LoaderInterface
/**
* Retrieves a branch alias (dev-master => 1.0.x-dev for example) if it exists
*
* @param array $config the entire package config
* @param mixed[] $config the entire package config
*
* @return string|null normalized version of the branch alias or null if there is none
*/
public function getBranchAlias(array $config)

View File

@ -24,6 +24,11 @@ class InvalidPackageException extends \Exception
/** @var mixed[] package config */
private $data;
/**
* @param string[] $errors
* @param string[] $warnings
* @param mixed[] $data
*/
public function __construct(array $errors, array $warnings, array $data)
{
$this->errors = $errors;
@ -32,16 +37,25 @@ class InvalidPackageException extends \Exception
parent::__construct("Invalid package information: \n".implode("\n", array_merge($errors, $warnings)));
}
/**
* @return mixed[]
*/
public function getData()
{
return $this->data;
}
/**
* @return string[]
*/
public function getErrors()
{
return $this->errors;
}
/**
* @return string[]
*/
public function getWarnings()
{
return $this->warnings;

View File

@ -15,7 +15,6 @@ namespace Composer\Package\Loader;
use Composer\Package\BasePackage;
use Composer\Config;
use Composer\IO\IOInterface;
use Composer\Package\Package;
use Composer\Package\RootAliasPackage;
use Composer\Repository\RepositoryFactory;
use Composer\Package\Version\VersionGuesser;
@ -58,9 +57,12 @@ class RootPackageLoader extends ArrayLoader
}
/**
* @inheritDoc
*
* @template PackageClass of RootPackage
*
* @param string $cwd cwd of the root package to be used to guess the version if it is not provided
* @param string|null $cwd
*
* @return RootPackage|RootAliasPackage
*
* @phpstan-param class-string<PackageClass> $class
@ -185,7 +187,10 @@ class RootPackageLoader extends ArrayLoader
}
/**
* @return array<array{package: string, version: string, alias: string, alias_normalized: string}>
* @param array<string, string> $requires
* @param list<array{package: string, version: string, alias: string, alias_normalized: string}> $aliases
*
* @return list<array{package: string, version: string, alias: string, alias_normalized: string}>
*/
private function extractAliases(array $requires, array $aliases)
{
@ -207,11 +212,20 @@ class RootPackageLoader extends ArrayLoader
/**
* @internal
* @return array<string, BasePackage::STABILITY_*>
*
* @param array<string, string> $requires
* @param string $minimumStability
* @param array<string, int> $stabilityFlags
*
* @return array<string, int>
*
* @phpstan-param array<string, BasePackage::STABILITY_*> $stabilityFlags
* @phpstan-return array<string, BasePackage::STABILITY_*>
*/
public static function extractStabilityFlags(array $requires, $minimumStability, array $stabilityFlags)
{
$stabilities = BasePackage::$stabilities;
/** @var int $minimumStability */
$minimumStability = $stabilities[$minimumStability];
foreach ($requires as $reqName => $reqVersion) {
$constraints = array();
@ -264,6 +278,10 @@ class RootPackageLoader extends ArrayLoader
/**
* @internal
*
* @param array<string, string> $requires
* @param array<string, string> $references
*
* @return array<string, string>
*/
public static function extractReferences(array $requires, array $references)

View File

@ -42,6 +42,10 @@ class ValidatingArrayLoader implements LoaderInterface
/** @var int One or more of self::CHECK_* constants */
private $flags;
/**
* @param bool $strictName
* @param int $flags
*/
public function __construct(LoaderInterface $loader, $strictName = true, VersionParser $parser = null, $flags = 0)
{
$this->loader = $loader;
@ -50,6 +54,9 @@ class ValidatingArrayLoader implements LoaderInterface
$this->flags = $flags;
}
/**
* @inheritDoc
*/
public function load(array $config, $class = 'Composer\Package\CompletePackage')
{
$this->errors = array();
@ -400,20 +407,32 @@ class ValidatingArrayLoader implements LoaderInterface
return $package;
}
/**
* @return string[]
*/
public function getWarnings()
{
return $this->warnings;
}
/**
* @return string[]
*/
public function getErrors()
{
return $this->errors;
}
/**
* @param string $name
* @param bool $isLink
*
* @return string|null
*/
public static function hasPackageNamingError($name, $isLink = false)
{
if (PlatformRepository::isPlatformPackage($name)) {
return;
return null;
}
if (!preg_match('{^[a-z0-9](?:[_.-]?[a-z0-9]+)*/[a-z0-9](?:(?:[_.]?|-{0,2})[a-z0-9]+)*$}iD', $name)) {
@ -440,8 +459,20 @@ class ValidatingArrayLoader implements LoaderInterface
return $name.' is invalid, it should not contain uppercase characters. We suggest using '.$suggestName.' instead.';
}
return null;
}
/**
* @param string $property
* @param string $regex
* @param bool $mandatory
*
* @return bool
*
* @phpstan-param non-empty-string $property
* @phpstan-param non-empty-string $regex
*/
private function validateRegex($property, $regex, $mandatory = false)
{
if (!$this->validateString($property, $mandatory)) {
@ -463,6 +494,14 @@ class ValidatingArrayLoader implements LoaderInterface
return true;
}
/**
* @param string $property
* @param bool $mandatory
*
* @return bool
*
* @phpstan-param non-empty-string $property
*/
private function validateString($property, $mandatory = false)
{
if (isset($this->config[$property]) && !is_string($this->config[$property])) {
@ -484,6 +523,14 @@ class ValidatingArrayLoader implements LoaderInterface
return true;
}
/**
* @param string $property
* @param bool $mandatory
*
* @return bool
*
* @phpstan-param non-empty-string $property
*/
private function validateArray($property, $mandatory = false)
{
if (isset($this->config[$property]) && !is_array($this->config[$property])) {
@ -505,6 +552,16 @@ class ValidatingArrayLoader implements LoaderInterface
return true;
}
/**
* @param string $property
* @param string|null $regex
* @param bool $mandatory
*
* @return bool
*
* @phpstan-param non-empty-string $property
* @phpstan-param non-empty-string|null $regex
*/
private function validateFlatArray($property, $regex = null, $mandatory = false)
{
if (!$this->validateArray($property, $mandatory)) {
@ -531,6 +588,14 @@ class ValidatingArrayLoader implements LoaderInterface
return $pass;
}
/**
* @param string $property
* @param bool $mandatory
*
* @return bool
*
* @phpstan-param non-empty-string $property
*/
private function validateUrl($property, $mandatory = false)
{
if (!$this->validateString($property, $mandatory)) {
@ -547,6 +612,12 @@ class ValidatingArrayLoader implements LoaderInterface
return true;
}
/**
* @param mixed $value
* @param string[] $schemes
*
* @return bool
*/
private function filterUrl($value, array $schemes = array('http', 'https'))
{
if ($value === '') {