minor #33112 [HttpFoundation] some cleanups (azjezz)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpFoundation] some cleanups

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

As found by @azjezz in #33088

Commits
-------

f62a3c0810 [HttpFoundation] some cleanups
This commit is contained in:
Nicolas Grekas 2019-08-11 11:24:24 +02:00
commit 17f15b3624
23 changed files with 40 additions and 70 deletions

View File

@ -153,7 +153,7 @@ class AcceptHeader
/** /**
* Sorts items by descending quality. * Sorts items by descending quality.
*/ */
private function sort() private function sort(): void
{ {
if (!$this->sorted) { if (!$this->sorted) {
uasort($this->items, function (AcceptHeaderItem $a, AcceptHeaderItem $b) { uasort($this->items, function (AcceptHeaderItem $a, AcceptHeaderItem $b) {

View File

@ -269,7 +269,7 @@ class BinaryFileResponse extends Response
return $this; return $this;
} }
private function hasValidIfRangeHeader(?string $header) private function hasValidIfRangeHeader(?string $header): bool
{ {
if ($this->getEtag() === $header) { if ($this->getEtag() === $header) {
return true; return true;

View File

@ -18,9 +18,6 @@ namespace Symfony\Component\HttpFoundation\File\Exception;
*/ */
class AccessDeniedException extends FileException class AccessDeniedException extends FileException
{ {
/**
* @param string $path The path to the accessed file
*/
public function __construct(string $path) public function __construct(string $path)
{ {
parent::__construct(sprintf('The file %s could not be accessed', $path)); parent::__construct(sprintf('The file %s could not be accessed', $path));

View File

@ -18,9 +18,6 @@ namespace Symfony\Component\HttpFoundation\File\Exception;
*/ */
class FileNotFoundException extends FileException class FileNotFoundException extends FileException
{ {
/**
* @param string $path The path to the file that was not found
*/
public function __construct(string $path) public function __construct(string $path)
{ {
parent::__construct(sprintf('The file "%s" does not exist', $path)); parent::__construct(sprintf('The file "%s" does not exist', $path));

View File

@ -99,6 +99,9 @@ class File extends \SplFileInfo
return $target; return $target;
} }
/**
* @return self
*/
protected function getTargetFile($directory, $name = null) protected function getTargetFile($directory, $name = null)
{ {
if (!is_dir($directory)) { if (!is_dir($directory)) {
@ -119,7 +122,7 @@ class File extends \SplFileInfo
* *
* @param string $name The new file name * @param string $name The new file name
* *
* @return string containing * @return string
*/ */
protected function getName($name) protected function getName($name)
{ {

View File

@ -24,7 +24,7 @@ class FileBag extends ParameterBag
private static $fileKeys = ['error', 'name', 'size', 'tmp_name', 'type']; private static $fileKeys = ['error', 'name', 'size', 'tmp_name', 'type'];
/** /**
* @param array $parameters An array of HTTP files * @param array|UploadedFile[] $parameters An array of HTTP files
*/ */
public function __construct(array $parameters = []) public function __construct(array $parameters = [])
{ {

View File

@ -21,9 +21,6 @@ class HeaderBag implements \IteratorAggregate, \Countable
protected $headers = []; protected $headers = [];
protected $cacheControl = []; protected $cacheControl = [];
/**
* @param array $headers An array of HTTP headers
*/
public function __construct(array $headers = []) public function __construct(array $headers = [])
{ {
foreach ($headers as $key => $values) { foreach ($headers as $key => $values) {
@ -85,8 +82,6 @@ class HeaderBag implements \IteratorAggregate, \Countable
/** /**
* Replaces the current HTTP headers by a new set. * Replaces the current HTTP headers by a new set.
*
* @param array $headers An array of HTTP headers
*/ */
public function replace(array $headers = []) public function replace(array $headers = [])
{ {
@ -96,8 +91,6 @@ class HeaderBag implements \IteratorAggregate, \Countable
/** /**
* Adds new headers the current HTTP headers set. * Adds new headers the current HTTP headers set.
*
* @param array $headers An array of HTTP headers
*/ */
public function add(array $headers) public function add(array $headers)
{ {
@ -204,10 +197,9 @@ class HeaderBag implements \IteratorAggregate, \Countable
/** /**
* Returns the HTTP header value converted to a date. * Returns the HTTP header value converted to a date.
* *
* @param string $key The parameter key * @param string $key The parameter key
* @param \DateTime $default The default value
* *
* @return \DateTime|null The parsed DateTime or the default value if the header does not exist * @return \DateTimeInterface|null The parsed DateTime or the default value if the header does not exist
* *
* @throws \RuntimeException When the HTTP header is not parseable * @throws \RuntimeException When the HTTP header is not parseable
*/ */

View File

@ -23,9 +23,6 @@ class ParameterBag implements \IteratorAggregate, \Countable
*/ */
protected $parameters; protected $parameters;
/**
* @param array $parameters An array of parameters
*/
public function __construct(array $parameters = []) public function __construct(array $parameters = [])
{ {
$this->parameters = $parameters; $this->parameters = $parameters;
@ -53,8 +50,6 @@ class ParameterBag implements \IteratorAggregate, \Countable
/** /**
* Replaces the current parameters by a new set. * Replaces the current parameters by a new set.
*
* @param array $parameters An array of parameters
*/ */
public function replace(array $parameters = []) public function replace(array $parameters = [])
{ {
@ -63,8 +58,6 @@ class ParameterBag implements \IteratorAggregate, \Countable
/** /**
* Adds parameters. * Adds parameters.
*
* @param array $parameters An array of parameters
*/ */
public function add(array $parameters = []) public function add(array $parameters = [])
{ {

View File

@ -632,7 +632,7 @@ class Request
*/ */
public static function normalizeQueryString($qs) public static function normalizeQueryString($qs)
{ {
if ('' == $qs) { if ('' === ($qs ?? '')) {
return ''; return '';
} }
@ -702,7 +702,7 @@ class Request
/** /**
* Gets the Session. * Gets the Session.
* *
* @return SessionInterface|null The session * @return SessionInterface The session
*/ */
public function getSession() public function getSession()
{ {
@ -1591,7 +1591,7 @@ class Request
/** /**
* Returns the preferred language. * Returns the preferred language.
* *
* @param array $locales An array of ordered available locales * @param string[] $locales An array of ordered available locales
* *
* @return string|null The preferred locale * @return string|null The preferred locale
*/ */
@ -1920,7 +1920,7 @@ class Request
]; ];
} }
private function setPhpDefaultLocale(string $locale) private function setPhpDefaultLocale(string $locale): void
{ {
// if either the class Locale doesn't exist, or an exception is thrown when // if either the class Locale doesn't exist, or an exception is thrown when
// setting the default locale, the intl module is not installed, and // setting the default locale, the intl module is not installed, and
@ -1982,7 +1982,7 @@ class Request
return self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR'), self::$trustedProxies); return self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR'), self::$trustedProxies);
} }
private function getTrustedValues(int $type, string $ip = null) private function getTrustedValues(int $type, string $ip = null): array
{ {
$clientValues = []; $clientValues = [];
$forwardedValues = []; $forwardedValues = [];
@ -2033,7 +2033,7 @@ class Request
throw new ConflictingHeadersException(sprintf('The request has both a trusted "%s" header and a trusted "%s" header, conflicting with each other. You should either configure your proxy to remove one of them, or configure your project to distrust the offending one.', self::$trustedHeaders[self::HEADER_FORWARDED], self::$trustedHeaders[$type])); throw new ConflictingHeadersException(sprintf('The request has both a trusted "%s" header and a trusted "%s" header, conflicting with each other. You should either configure your proxy to remove one of them, or configure your project to distrust the offending one.', self::$trustedHeaders[self::HEADER_FORWARDED], self::$trustedHeaders[$type]));
} }
private function normalizeAndFilterClientIps(array $clientIps, string $ip) private function normalizeAndFilterClientIps(array $clientIps, string $ip): array
{ {
if (!$clientIps) { if (!$clientIps) {
return []; return [];

View File

@ -54,11 +54,8 @@ class RequestMatcher implements RequestMatcherInterface
private $schemes = []; private $schemes = [];
/** /**
* @param string|null $path
* @param string|null $host
* @param string|string[]|null $methods * @param string|string[]|null $methods
* @param string|string[]|null $ips * @param string|string[]|null $ips
* @param array $attributes
* @param string|string[]|null $schemes * @param string|string[]|null $schemes
*/ */
public function __construct(string $path = null, string $host = null, $methods = null, $ips = null, array $attributes = [], $schemes = null, int $port = null) public function __construct(string $path = null, string $host = null, $methods = null, $ips = null, array $attributes = [], $schemes = null, int $port = null)

View File

@ -1208,7 +1208,7 @@ class Response
* *
* @final * @final
*/ */
public static function closeOutputBuffers(int $targetLevel, bool $flush) public static function closeOutputBuffers(int $targetLevel, bool $flush): void
{ {
$status = ob_get_status(true); $status = ob_get_status(true);
$level = \count($status); $level = \count($status);
@ -1230,7 +1230,7 @@ class Response
* *
* @final * @final
*/ */
protected function ensureIEOverSSLCompatibility(Request $request) protected function ensureIEOverSSLCompatibility(Request $request): void
{ {
if (false !== stripos($this->headers->get('Content-Disposition'), 'attachment') && 1 == preg_match('/MSIE (.*?);/i', $request->server->get('HTTP_USER_AGENT'), $match) && true === $request->isSecure()) { if (false !== stripos($this->headers->get('Content-Disposition'), 'attachment') && 1 == preg_match('/MSIE (.*?);/i', $request->server->get('HTTP_USER_AGENT'), $match) && true === $request->isSecure()) {
if ((int) preg_replace('/(MSIE )(.*?);/', '$2', $match[0]) < 9) { if ((int) preg_replace('/(MSIE )(.*?);/', '$2', $match[0]) < 9) {

View File

@ -298,7 +298,7 @@ class ResponseHeaderBag extends HeaderBag
return $header; return $header;
} }
private function initDate() private function initDate(): void
{ {
$now = \DateTime::createFromFormat('U', time()); $now = \DateTime::createFromFormat('U', time());
$now->setTimezone(new \DateTimeZone('UTC')); $now->setTimezone(new \DateTimeZone('UTC'));

View File

@ -50,15 +50,10 @@ interface AttributeBagInterface extends SessionBagInterface
/** /**
* Returns attributes. * Returns attributes.
* *
* @return array Attributes * @return array
*/ */
public function all(); public function all();
/**
* Sets attributes.
*
* @param array $attributes Attributes
*/
public function replace(array $attributes); public function replace(array $attributes);
/** /**

View File

@ -129,7 +129,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
/** /**
* Returns the number of attributes. * Returns the number of attributes.
* *
* @return int The number of attributes * @return int
*/ */
public function count() public function count()
{ {

View File

@ -63,7 +63,7 @@ final class SessionBagProxy implements SessionBagInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function initialize(array &$array) public function initialize(array &$array): void
{ {
++$this->usageIndex; ++$this->usageIndex;
$this->data[$this->bag->getStorageKey()] = &$array; $this->data[$this->bag->getStorageKey()] = &$array;

View File

@ -23,7 +23,7 @@ interface SessionInterface
/** /**
* Starts the session storage. * Starts the session storage.
* *
* @return bool True if session started * @return bool
* *
* @throws \RuntimeException if session fails to start * @throws \RuntimeException if session fails to start
*/ */
@ -32,7 +32,7 @@ interface SessionInterface
/** /**
* Returns the session ID. * Returns the session ID.
* *
* @return string The session ID * @return string
*/ */
public function getId(); public function getId();
@ -46,7 +46,7 @@ interface SessionInterface
/** /**
* Returns the session name. * Returns the session name.
* *
* @return mixed The session name * @return string
*/ */
public function getName(); public function getName();
@ -68,7 +68,7 @@ interface SessionInterface
* to expire with browser session. Time is in seconds, and is * to expire with browser session. Time is in seconds, and is
* not a Unix timestamp. * not a Unix timestamp.
* *
* @return bool True if session invalidated, false if error * @return bool
*/ */
public function invalidate($lifetime = null); public function invalidate($lifetime = null);
@ -82,7 +82,7 @@ interface SessionInterface
* to expire with browser session. Time is in seconds, and is * to expire with browser session. Time is in seconds, and is
* not a Unix timestamp. * not a Unix timestamp.
* *
* @return bool True if session migrated, false if error * @return bool
*/ */
public function migrate($destroy = false, $lifetime = null); public function migrate($destroy = false, $lifetime = null);
@ -100,7 +100,7 @@ interface SessionInterface
* *
* @param string $name The attribute name * @param string $name The attribute name
* *
* @return bool true if the attribute is defined, false otherwise * @return bool
*/ */
public function has($name); public function has($name);
@ -125,14 +125,12 @@ interface SessionInterface
/** /**
* Returns attributes. * Returns attributes.
* *
* @return array Attributes * @return array
*/ */
public function all(); public function all();
/** /**
* Sets attributes. * Sets attributes.
*
* @param array $attributes Attributes
*/ */
public function replace(array $attributes); public function replace(array $attributes);

View File

@ -40,8 +40,6 @@ class MemcachedSessionHandler extends AbstractSessionHandler
* * prefix: The prefix to use for the memcached keys in order to avoid collision * * prefix: The prefix to use for the memcached keys in order to avoid collision
* * expiretime: The time to live in seconds. * * expiretime: The time to live in seconds.
* *
* @param \Memcached $memcached A \Memcached instance
*
* @throws \InvalidArgumentException When unsupported options are passed * @throws \InvalidArgumentException When unsupported options are passed
*/ */
public function __construct(\Memcached $memcached, array $options = []) public function __construct(\Memcached $memcached, array $options = [])

View File

@ -423,7 +423,7 @@ class PdoSessionHandler extends AbstractSessionHandler
/** /**
* Lazy-connects to the database. * Lazy-connects to the database.
*/ */
private function connect(string $dsn) private function connect(string $dsn): void
{ {
$this->pdo = new \PDO($dsn, $this->username, $this->password, $this->connectionOptions); $this->pdo = new \PDO($dsn, $this->username, $this->password, $this->connectionOptions);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
@ -534,7 +534,7 @@ class PdoSessionHandler extends AbstractSessionHandler
* due to https://percona.com/blog/2013/12/12/one-more-innodb-gap-lock-to-avoid/ . * due to https://percona.com/blog/2013/12/12/one-more-innodb-gap-lock-to-avoid/ .
* So we change it to READ COMMITTED. * So we change it to READ COMMITTED.
*/ */
private function beginTransaction() private function beginTransaction(): void
{ {
if (!$this->inTransaction) { if (!$this->inTransaction) {
if ('sqlite' === $this->driver) { if ('sqlite' === $this->driver) {
@ -552,7 +552,7 @@ class PdoSessionHandler extends AbstractSessionHandler
/** /**
* Helper method to commit a transaction. * Helper method to commit a transaction.
*/ */
private function commit() private function commit(): void
{ {
if ($this->inTransaction) { if ($this->inTransaction) {
try { try {
@ -574,7 +574,7 @@ class PdoSessionHandler extends AbstractSessionHandler
/** /**
* Helper method to rollback a transaction. * Helper method to rollback a transaction.
*/ */
private function rollback() private function rollback(): void
{ {
// We only need to rollback if we are in a transaction. Otherwise the resulting // We only need to rollback if we are in a transaction. Otherwise the resulting
// error would hide the real problem why rollback was called. We might not be // error would hide the real problem why rollback was called. We might not be

View File

@ -159,7 +159,7 @@ class MetadataBag implements SessionBagInterface
$this->name = $name; $this->name = $name;
} }
private function stampCreated(int $lifetime = null) private function stampCreated(int $lifetime = null): void
{ {
$timeStamp = time(); $timeStamp = time();
$this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp; $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;

View File

@ -121,7 +121,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
* Deletes a session from persistent storage. * Deletes a session from persistent storage.
* Deliberately leaves session data in memory intact. * Deliberately leaves session data in memory intact.
*/ */
private function destroy() private function destroy(): void
{ {
if (is_file($this->getFilePath())) { if (is_file($this->getFilePath())) {
unlink($this->getFilePath()); unlink($this->getFilePath());
@ -139,7 +139,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
/** /**
* Reads session from storage and loads session. * Reads session from storage and loads session.
*/ */
private function read() private function read(): void
{ {
$filePath = $this->getFilePath(); $filePath = $this->getFilePath();
$this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : []; $this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : [];

View File

@ -97,7 +97,7 @@ class NativeSessionStorage implements SessionStorageInterface
* trans_sid_hosts, $_SERVER['HTTP_HOST'] * trans_sid_hosts, $_SERVER['HTTP_HOST']
* trans_sid_tags, "a=href,area=href,frame=src,form=" * trans_sid_tags, "a=href,area=href,frame=src,form="
* *
* @param \SessionHandlerInterface|null $handler * @param AbstractProxy|\SessionHandlerInterface|null $handler
*/ */
public function __construct(array $options = [], $handler = null, MetadataBag $metaBag = null) public function __construct(array $options = [], $handler = null, MetadataBag $metaBag = null)
{ {
@ -409,7 +409,7 @@ class NativeSessionStorage implements SessionStorageInterface
* @see https://php.net/sessionhandler * @see https://php.net/sessionhandler
* @see https://github.com/zikula/NativeSession * @see https://github.com/zikula/NativeSession
* *
* @param \SessionHandlerInterface|null $saveHandler * @param AbstractProxy|\SessionHandlerInterface|null $saveHandler
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\HttpFoundation\Session\Storage; namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
/** /**
* Allows session to be started by PHP and managed by Symfony. * Allows session to be started by PHP and managed by Symfony.
* *
@ -19,7 +21,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage;
class PhpBridgeSessionStorage extends NativeSessionStorage class PhpBridgeSessionStorage extends NativeSessionStorage
{ {
/** /**
* @param \SessionHandlerInterface|null $handler * @param AbstractProxy|\SessionHandlerInterface|null $handler
*/ */
public function __construct($handler = null, MetadataBag $metaBag = null) public function __construct($handler = null, MetadataBag $metaBag = null)
{ {

View File

@ -63,8 +63,6 @@ class StreamedResponse extends Response
/** /**
* Sets the PHP callback associated with this Response. * Sets the PHP callback associated with this Response.
* *
* @param callable $callback A valid PHP callback
*
* @return $this * @return $this
*/ */
public function setCallback(callable $callback) public function setCallback(callable $callback)