Skip to content

Commit

Permalink
minor #32202 [Asset] Add type-hints to public interfaces and classes …
Browse files Browse the repository at this point in the history
…(jschaedl)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[Asset] 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 Asset component.

Commits
-------

065fdc1 [Asset] Add type-hints to public interfaces
  • Loading branch information
fabpot committed Jun 29, 2019
2 parents 0781b60 + 065fdc1 commit bac35b0
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/Asset/Package.php
Expand Up @@ -35,15 +35,15 @@ public function __construct(VersionStrategyInterface $versionStrategy, ContextIn
/**
* {@inheritdoc}
*/
public function getVersion($path)
public function getVersion(string $path)
{
return $this->versionStrategy->getVersion($path);
}

/**
* {@inheritdoc}
*/
public function getUrl($path)
public function getUrl(string $path)
{
if ($this->isAbsoluteUrl($path)) {
return $path;
Expand All @@ -68,7 +68,7 @@ protected function getVersionStrategy()
return $this->versionStrategy;
}

protected function isAbsoluteUrl($url)
protected function isAbsoluteUrl(string $url)
{
return false !== strpos($url, '://') || '//' === substr($url, 0, 2);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Symfony/Component/Asset/PackageInterface.php
Expand Up @@ -21,18 +21,14 @@ interface PackageInterface
/**
* Returns the asset version for an asset.
*
* @param string $path A path
*
* @return string The version string
*/
public function getVersion($path);
public function getVersion(string $path);

/**
* Returns an absolute or root-relative public path.
*
* @param string $path A path
*
* @return string The public path
*/
public function getUrl($path);
public function getUrl(string $path);
}
13 changes: 5 additions & 8 deletions src/Symfony/Component/Asset/Packages.php
Expand Up @@ -44,12 +44,9 @@ public function setDefaultPackage(PackageInterface $defaultPackage)
}

/**
* Adds a package.
*
* @param string $name The package name
* @param PackageInterface $package The package
* Adds a package.
*/
public function addPackage($name, PackageInterface $package)
public function addPackage(string $name, PackageInterface $package)
{
$this->packages[$name] = $package;
}
Expand All @@ -64,7 +61,7 @@ public function addPackage($name, PackageInterface $package)
* @throws InvalidArgumentException If there is no package by that name
* @throws LogicException If no default package is defined
*/
public function getPackage($name = null)
public function getPackage(string $name = null)
{
if (null === $name) {
if (null === $this->defaultPackage) {
Expand All @@ -89,7 +86,7 @@ public function getPackage($name = null)
*
* @return string The current version
*/
public function getVersion($path, $packageName = null)
public function getVersion(string $path, string $packageName = null)
{
return $this->getPackage($packageName)->getVersion($path);
}
Expand All @@ -104,7 +101,7 @@ public function getVersion($path, $packageName = null)
*
* @return string A public path which takes into account the base path and URL path
*/
public function getUrl($path, $packageName = null)
public function getUrl(string $path, string $packageName = null)
{
return $this->getPackage($packageName)->getUrl($path);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Asset/PathPackage.php
Expand Up @@ -51,7 +51,7 @@ public function __construct(string $basePath, VersionStrategyInterface $versionS
/**
* {@inheritdoc}
*/
public function getUrl($path)
public function getUrl(string $path)
{
$versionedPath = parent::getUrl($path);

Expand Down
12 changes: 4 additions & 8 deletions src/Symfony/Component/Asset/UrlPackage.php
Expand Up @@ -69,7 +69,7 @@ public function __construct($baseUrls, VersionStrategyInterface $versionStrategy
/**
* {@inheritdoc}
*/
public function getUrl($path)
public function getUrl(string $path)
{
if ($this->isAbsoluteUrl($path)) {
return $path;
Expand All @@ -95,11 +95,9 @@ public function getUrl($path)
/**
* Returns the base URL for a path.
*
* @param string $path
*
* @return string The base URL
*/
public function getBaseUrl($path)
public function getBaseUrl(string $path)
{
if (1 === \count($this->baseUrls)) {
return $this->baseUrls[0];
Expand All @@ -114,16 +112,14 @@ public function getBaseUrl($path)
* Override this method to change the default distribution strategy.
* This method should always return the same base URL index for a given path.
*
* @param string $path
*
* @return int The base URL index for the given path
*/
protected function chooseBaseUrl($path)
protected function chooseBaseUrl(string $path)
{
return (int) fmod(hexdec(substr(hash('sha256', $path), 0, 10)), \count($this->baseUrls));
}

private function getSslUrls($urls)
private function getSslUrls(array $urls)
{
$sslUrls = [];
foreach ($urls as $url) {
Expand Down
Expand Up @@ -21,15 +21,15 @@ class EmptyVersionStrategy implements VersionStrategyInterface
/**
* {@inheritdoc}
*/
public function getVersion($path)
public function getVersion(string $path)
{
return '';
}

/**
* {@inheritdoc}
*/
public function applyVersion($path)
public function applyVersion(string $path)
{
return $path;
}
Expand Down
Expand Up @@ -40,17 +40,17 @@ public function __construct(string $manifestPath)
* the version is. Instead, this returns the path to the
* versioned file.
*/
public function getVersion($path)
public function getVersion(string $path)
{
return $this->applyVersion($path);
}

public function applyVersion($path)
public function applyVersion(string $path)
{
return $this->getManifestPath($path) ?: $path;
}

private function getManifestPath($path)
private function getManifestPath(string $path)
{
if (null === $this->manifestData) {
if (!file_exists($this->manifestPath)) {
Expand Down
Expand Up @@ -34,15 +34,15 @@ public function __construct(string $version, string $format = null)
/**
* {@inheritdoc}
*/
public function getVersion($path)
public function getVersion(string $path)
{
return $this->version;
}

/**
* {@inheritdoc}
*/
public function applyVersion($path)
public function applyVersion(string $path)
{
$versionized = sprintf($this->format, ltrim($path, '/'), $this->getVersion($path));

Expand Down
Expand Up @@ -21,18 +21,14 @@ interface VersionStrategyInterface
/**
* Returns the asset version for an asset.
*
* @param string $path A path
*
* @return string The version string
*/
public function getVersion($path);
public function getVersion(string $path);

/**
* Applies version to the supplied path.
*
* @param string $path A path
*
* @return string The versionized path
*/
public function applyVersion($path);
public function applyVersion(string $path);
}

0 comments on commit bac35b0

Please sign in to comment.