8000 [HttpFoundation] Add type-hints by julien57 · Pull Request #32271 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation] Add type-hints #32271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions src/Symfony/Component/HttpFoundation/AcceptHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ public function __construct(array $items)
/**
* Builds an AcceptHeader instance from a string.
*
* @param string $headerValue
*
* @return self
*/
public static function fromString($headerValue)
public static function fromString(?string $headerValue)
{
$index = 0;

$parts = HeaderUtils::split((string) $headerValue, ',;=');
$parts = HeaderUtils::split($headerValue ?? '', ',;=');

return new self(array_map(function ($subParts) use (&$index) {
$part = array_shift($subParts);
Expand All @@ -78,23 +76,19 @@ public function __toString()
/**
* Tests if header has given value.
*
* @param string $value
*
* @return bool
*/
public function has($value)
public function has(string $value)
{
return isset($this->items[$value]);
}

/**
* Returns given value's item, if exists.
*
* @param string $value
*
* @return AcceptHeaderItem|null
*/
public function get($value)
public function get(string $value)
{
return $this->items[$value] ?? $this->items[explode('/', $value)[0].'/*'] ?? $this->items['*/*'] ?? $this->items['*'] ?? null;
}
Expand Down Expand Up @@ -127,11 +121,9 @@ public function all()
/**
* Filters items on their value using given regex.
*
* @param string $pattern
*
* @return self
*/
public function filter($pattern)
public function filter(string $pattern)
{
return new self(array_filter($this->items, function (AcceptHeaderItem $item) use ($pattern) {
return preg_match($pattern, $item->getValue());
Expand Down
30 changes: 8 additions & 22 deletions src/Symfony/Component/HttpFoundation/AcceptHeaderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ public function __construct(string $value, array $attributes = [])
/**
* Builds an AcceptHeaderInstance instance from a string.
*
* @param string $itemValue
*
* @return self
*/
public static function fromString($itemValue)
public static function fromString(string $itemValue)
{
$parts = HeaderUtils::split($itemValue, ';=');

Expand Down Expand Up @@ -66,11 +64,9 @@ public function __toString()
/**
* Set the item value.
*
* @param string $value
*
* @return $this
*/
public function setValue($value)
public function setValue(string $value)
{
$this->value = $value;

Expand All @@ -90,11 +86,9 @@ public function getValue()
/**
* Set the item quality.
*
* @param float $quality
*
* @return $this
*/
public function setQuality($quality)
public function setQuality(float $quality)
{
$this->quality = $quality;

Expand All @@ -114,11 +108,9 @@ public function getQuality()
/**
* Set the item index.
*
* @param int $index
*
* @return $this
*/
public function setIndex($index)
public function setIndex(int $index)
{
$this->index = $index;

Expand All @@ -138,24 +130,21 @@ public function getIndex()
/**
* Tests if an attribute exists.
*
* @param string $name
*
* @return bool
*/
public function hasAttribute($name)
public function hasAttribute(string $name)
{
return isset($this->attributes[$name]);
}

/**
* Returns an attribute by its name.
*
* @param string $name
* @param mixed $default
* @param mixed $default
*
* @return mixed
*/
public function getAttribute($name, $default = null)
public function getAttribute(string $name, $default = null)
{
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
}
Expand All @@ -173,12 +162,9 @@ public function getAttributes()
/**
* Set an attribute.
*
* @param string $name
* @param string $value
*
* @return $this
*/
public function setAttribute($name, $value)
public function setAttribute(string $name, string $value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is still a (string) $value cast left in the implementation

{
if ('q' === $name) {
$this->quality = (float) $value;
Expand Down
15 changes: 5 additions & 10 deletions src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,21 @@ public function __construct($file, int $status = 200, array $headers = [], bool
*
* @return static
*/
public static function create($file = null, $status = 200, $headers = [], $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
public static function create($file = null, int $status = 200, array $headers = [], bool $public = true, string $contentDisposition = null, bool $autoEtag = false, bool $autoLa 10000 stModified = true)
{
return new static($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified);
}

/**
* Sets the file to stream.
*
* @param \SplFileInfo|string $file The file to stream
* @param string $contentDisposition
* @param bool $autoEtag
* @param bool $autoLastModified
* @param \SplFileInfo|string $file The file to stream
*
* @return $this
*
* @throws FileException
*/
public function setFile($file, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
public function setFile($file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
{
if (!$file instanceof File) {
if ($file instanceof \SplFileInfo) {
Expand Down Expand Up @@ -153,7 +150,7 @@ public function setAutoEtag()
*
* @return $this
*/
public function setContentDisposition($disposition, $filename = '', $filenameFallback = '')
public function setContentDisposition(string $disposition, string $filename = '', string $filenameFallback = '')
{
if ('' === $filename) {
$filename = $this->file->getFilename();
Expand Down Expand Up @@ -346,11 +343,9 @@ public static function trustXSendfileTypeHeader()
* If this is set to true, the file will be unlinked after the request is send
* Note: If the X-Sendfile header is used, the deleteFileAfterSend setting will not be used.
*
* @param bool $shouldDelete
*
* @return $this
*/
public function deleteFileAfterSend($shouldDelete = true)
public function deleteFileAfterSend(bool $shouldDelete = true)
{
$this->deleteFileAfterSend = $shouldDelete;

Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Component/HttpFoundation/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ class Cookie
/**
* Creates cookie from raw header string.
*
* @param string $cookie
* @param bool $decode
*
* @return static
*/
public static function fromString($cookie, $decode = false)
public static function fromString(string $cookie, bool $decode = false)
{
$data = [
'expires' => 0,
Expand Down Expand Up @@ -74,8 +71,6 @@ public static function create(string $name, string $value = null, $expire = 0, ?
}

/**
* @param string $name The name of the cookie
* @param string|null $value The value of the cookie
* @param int|string|\DateTimeInterface $expire The time the cookie expires
* @param string $path The path on the server in which the cookie will be available on
* @param string|null $domain The domain that the cookie is available to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
class AccessDeniedException extends FileException
{
/**
* @param string $path The path to the accessed file
*/
public function __co A851 nstruct(string $path)
{
parent::__construct(sprintf('The file %s could not be accessed', $path));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
class FileNotFoundException extends FileException
{
/**
* @param string $path The path to the file that was not found
*/
public function __construct(string $path)
{
parent::__construct(sprintf('The file "%s" does not exist', $path));
Expand Down
12 changes: 2 additions & 10 deletions src/Symfony/Component/HttpFoundation/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class File extends \SplFileInfo
/**
* Constructs a new file from the given path.
*
* @param string $path The path to the file
* @param bool $checkPath Whether to check the path or not
*
* @throws FileNotFoundException If the given path is not a file
*/
public function __construct(string $path, bool $checkPath = true)
Expand Down Expand Up @@ -76,14 +73,11 @@ public function getMimeType()
/**
* Moves the file to a new location.
*
* @param string $directory The destination folder
* @param string $name The new file name
*
* @return self A File object representing the new file
*
* @throws FileException if the target file could not be created
*/
public function move($directory, $name = null)
public function move(string $directory, string $name = null)
{
$target = $this->getTargetFile($directory, $name);

Expand Down Expand Up @@ -117,11 +111,9 @@ protected function getTargetFile($directory, $name = null)
/**
* Returns locale independent base name of the given path.
*
* @param string $name The new file name
*
* @return string containing
*/
protected function getName($name)
protected function getName(string $name)
{
$originalName = str_replace('\\', '/', $name);
$pos = strrpos($originalName, '/');
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,11 @@ public function isValid()
/**
* Moves the file to a new location.
*
* @param string $directory The destination folder
* @param string $name The new file name
*
* @return File A File object representing the new file
*
* @throws FileException if, for any reason, the file could not have been moved
*/
public function move($directory, $name = null)
public function move(string $directory, string $name = null)
{
if ($this->isValid()) {
if ($this->test) {
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/HttpFoundation/FileBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class FileBag extends ParameterBag
{
private static $fileKeys = ['error', 'name', 'size', 'tmp_name', 'type'];

/**
* @param array $parameters An array of HTTP files
*/
public function __construct(array $parameters = [])
{
$this->replace($parameters);
Expand All @@ -43,7 +40,7 @@ public function replace(array $files = [])
/**
* {@inheritdoc}
*/
public function set($key, $value)
public function set(string $key, $value)
{
if (!\is_array($value) && !$value instanceof UploadedFile) {
throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
Expand Down
Loading
0