minor #32201 [Config] Add type-hints to public interfaces and classes (jschaedl)

This PR was squashed before being merged into the 5.0-dev branch (closes #32201).

Discussion
----------

[Config] Add type-hints to public interfaces and classes

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see below -->
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #32179  <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A <!-- required for new features -->

This PR adds type hints to all public interfaces of the Config component.

Commits
-------

e069dc4eda [Config] Add type-hints to public interfaces and classes
This commit is contained in:
Fabien Potencier 2019-06-29 08:48:52 +02:00
commit 77398495ea
18 changed files with 35 additions and 45 deletions

View File

@ -35,7 +35,7 @@ class ConfigCacheFactory implements ConfigCacheFactoryInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function cache($file, $callback) public function cache(string $file, $callback)
{ {
if (!\is_callable($callback)) { if (!\is_callable($callback)) {
throw new \InvalidArgumentException(sprintf('Invalid type for callback argument. Expected callable, but got "%s".', \gettype($callback))); throw new \InvalidArgumentException(sprintf('Invalid type for callback argument. Expected callable, but got "%s".', \gettype($callback)));

View File

@ -28,5 +28,5 @@ interface ConfigCacheFactoryInterface
* *
* @return ConfigCacheInterface The cache instance * @return ConfigCacheInterface The cache instance
*/ */
public function cache($file, $callable); public function cache(string $file, $callable);
} }

View File

@ -45,5 +45,5 @@ interface ConfigCacheInterface
* *
* @throws \RuntimeException When the cache file cannot be written * @throws \RuntimeException When the cache file cannot be written
*/ */
public function write($content, array $metadata = null); public function write(string $content, array $metadata = null);
} }

View File

@ -102,40 +102,32 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
/** /**
* Sets whether to add default values for this array if it has not been * Sets whether to add default values for this array if it has not been
* defined in any of the configuration files. * defined in any of the configuration files.
*
* @param bool $boolean
*/ */
public function setAddIfNotSet($boolean) public function setAddIfNotSet(bool $boolean)
{ {
$this->addIfNotSet = (bool) $boolean; $this->addIfNotSet = $boolean;
} }
/** /**
* Sets whether false is allowed as value indicating that the array should be unset. * Sets whether false is allowed as value indicating that the array should be unset.
*
* @param bool $allow
*/ */
public function setAllowFalse($allow) public function setAllowFalse(bool $allow)
{ {
$this->allowFalse = (bool) $allow; $this->allowFalse = $allow;
} }
/** /**
* Sets whether new keys can be defined in subsequent configurations. * Sets whether new keys can be defined in subsequent configurations.
*
* @param bool $allow
*/ */
public function setAllowNewKeys($allow) public function setAllowNewKeys(bool $allow)
{ {
$this->allowNewKeys = (bool) $allow; $this->allowNewKeys = $allow;
} }
/** /**
* Sets if deep merging should occur. * Sets if deep merging should occur.
*
* @param bool $boolean
*/ */
public function setPerformDeepMerging($boolean) public function setPerformDeepMerging(bool $boolean)
{ {
$this->performDeepMerging = (bool) $boolean; $this->performDeepMerging = (bool) $boolean;
} }
@ -146,16 +138,16 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
* @param bool $boolean To allow extra keys * @param bool $boolean To allow extra keys
* @param bool $remove To remove extra keys * @param bool $remove To remove extra keys
*/ */
public function setIgnoreExtraKeys($boolean, $remove = true) public function setIgnoreExtraKeys(bool $boolean, $remove = true)
{ {
$this->ignoreExtraKeys = (bool) $boolean; $this->ignoreExtraKeys = $boolean;
$this->removeExtraKeys = $this->ignoreExtraKeys && $remove; $this->removeExtraKeys = $this->ignoreExtraKeys && $remove;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setName($name) public function setName(string $name)
{ {
$this->name = $name; $this->name = $name;
} }

View File

@ -23,5 +23,5 @@ interface PrototypeNodeInterface extends NodeInterface
* *
* @param string $name The name of the node * @param string $name The name of the node
*/ */
public function setName($name); public function setName(string $name);
} }

View File

@ -56,15 +56,15 @@ class VariableNode extends BaseNode implements PrototypeNodeInterface
* *
* @param bool $boolean True if this entity will accept empty values * @param bool $boolean True if this entity will accept empty values
*/ */
public function setAllowEmptyValue($boolean) public function setAllowEmptyValue(bool $boolean)
{ {
$this->allowEmptyValue = (bool) $boolean; $this->allowEmptyValue = $boolean;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setName($name) public function setName(string $name)
{ {
$this->name = $name; $this->name = $name;
} }

View File

@ -33,7 +33,7 @@ class FileLocator implements FileLocatorInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function locate($name, $currentPath = null, $first = true) public function locate(string $name, string $currentPath = null, $first = true)
{ {
if ('' == $name) { if ('' == $name) {
throw new \InvalidArgumentException('An empty file name is not valid to be located.'); throw new \InvalidArgumentException('An empty file name is not valid to be located.');
@ -81,7 +81,7 @@ class FileLocator implements FileLocatorInterface
* *
* @return bool * @return bool
*/ */
private function isAbsolutePath($file) private function isAbsolutePath(string $file)
{ {
if ('/' === $file[0] || '\\' === $file[0] if ('/' === $file[0] || '\\' === $file[0]
|| (\strlen($file) > 3 && ctype_alpha($file[0]) || (\strlen($file) > 3 && ctype_alpha($file[0])

View File

@ -30,5 +30,5 @@ interface FileLocatorInterface
* @throws \InvalidArgumentException If $name is empty * @throws \InvalidArgumentException If $name is empty
* @throws FileLocatorFileNotFoundException If a file is not found * @throws FileLocatorFileNotFoundException If a file is not found
*/ */
public function locate($name, $currentPath = null, $first = true); public function locate(string $name, string $currentPath = null, bool $first = true);
} }

View File

@ -31,7 +31,7 @@ class DelegatingLoader extends Loader
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function load($resource, $type = null) public function load($resource, string $type = null)
{ {
if (false === $loader = $this->resolver->resolve($resource, $type)) { if (false === $loader = $this->resolver->resolve($resource, $type)) {
throw new LoaderLoadException($resource, null, null, null, $type); throw new LoaderLoadException($resource, null, null, null, $type);
@ -43,7 +43,7 @@ class DelegatingLoader extends Loader
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supports($resource, $type = null) public function supports($resource, string $type = null)
{ {
return false !== $this->resolver->resolve($resource, $type); return false !== $this->resolver->resolve($resource, $type);
} }

View File

@ -21,7 +21,7 @@ class GlobFileLoader extends FileLoader
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function load($resource, $type = null) public function load($resource, string $type = null)
{ {
return $this->import($resource); return $this->import($resource);
} }
@ -29,7 +29,7 @@ class GlobFileLoader extends FileLoader
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supports($resource, $type = null) public function supports($resource, string $type = null)
{ {
return 'glob' === $type; return 'glob' === $type;
} }

View File

@ -22,21 +22,19 @@ interface LoaderInterface
* Loads a resource. * Loads a resource.
* *
* @param mixed $resource The resource * @param mixed $resource The resource
* @param string|null $type The resource type or null if unknown
* *
* @throws \Exception If something went wrong * @throws \Exception If something went wrong
*/ */
public function load($resource, $type = null); public function load($resource, string $type = null);
/** /**
* Returns whether this class supports the given resource. * Returns whether this class supports the given resource.
* *
* @param mixed $resource A resource * @param mixed $resource A resource
* @param string|null $type The resource type or null if unknown
* *
* @return bool True if this class supports the given resource, false otherwise * @return bool True if this class supports the given resource, false otherwise
*/ */
public function supports($resource, $type = null); public function supports($resource, string $type = null);
/** /**
* Gets the loader resolver. * Gets the loader resolver.

View File

@ -39,7 +39,7 @@ class LoaderResolver implements LoaderResolverInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function resolve($resource, $type = null) public function resolve($resource, string $type = null)
{ {
foreach ($this->loaders as $loader) { foreach ($this->loaders as $loader) {
if ($loader->supports($resource, $type)) { if ($loader->supports($resource, $type)) {

View File

@ -26,5 +26,5 @@ interface LoaderResolverInterface
* *
* @return LoaderInterface|false The loader or false if none is able to load the resource * @return LoaderInterface|false The loader or false if none is able to load the resource
*/ */
public function resolve($resource, $type = null); public function resolve($resource, string $type = null);
} }

View File

@ -28,7 +28,7 @@ class SelfCheckingResourceChecker implements ResourceCheckerInterface
return $metadata instanceof SelfCheckingResourceInterface; return $metadata instanceof SelfCheckingResourceInterface;
} }
public function isFresh(ResourceInterface $resource, $timestamp) public function isFresh(ResourceInterface $resource, int $timestamp)
{ {
/* @var SelfCheckingResourceInterface $resource */ /* @var SelfCheckingResourceInterface $resource */
return $resource->isFresh($timestamp); return $resource->isFresh($timestamp);

View File

@ -116,7 +116,7 @@ class ResourceCheckerConfigCache implements ConfigCacheInterface
* *
* @throws \RuntimeException When cache file can't be written * @throws \RuntimeException When cache file can't be written
*/ */
public function write($content, array $metadata = null) public function write(string $content, array $metadata = null)
{ {
$mode = 0666; $mode = 0666;
$umask = umask(); $umask = umask();

View File

@ -32,7 +32,7 @@ class ResourceCheckerConfigCacheFactory implements ConfigCacheFactoryInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function cache($file, $callback) public function cache(string $file, $callback)
{ {
if (!\is_callable($callback)) { if (!\is_callable($callback)) {
throw new \InvalidArgumentException(sprintf('Invalid type for callback argument. Expected callable, but got "%s".', \gettype($callback))); throw new \InvalidArgumentException(sprintf('Invalid type for callback argument. Expected callable, but got "%s".', \gettype($callback)));

View File

@ -44,5 +44,5 @@ interface ResourceCheckerInterface
* *
* @return bool True if the resource has not changed since the given timestamp, false otherwise * @return bool True if the resource has not changed since the given timestamp, false otherwise
*/ */
public function isFresh(ResourceInterface $resource, $timestamp); public function isFresh(ResourceInterface $resource, int $timestamp);
} }

View File

@ -115,6 +115,6 @@ class FileLocatorTest extends TestCase
{ {
$loader = new FileLocator([__DIR__.'/Fixtures']); $loader = new FileLocator([__DIR__.'/Fixtures']);
$loader->locate(null, __DIR__); $loader->locate('', __DIR__);
} }
} }