Skip to content

Commit

Permalink
add type-hints
Browse files Browse the repository at this point in the history
  • Loading branch information
julien57 authored and nicolas-grekas committed Aug 11, 2019
1 parent 9fec71e commit 61da067
Show file tree
Hide file tree
Showing 42 changed files with 206 additions and 453 deletions.
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
34 changes: 10 additions & 24 deletions src/Symfony/Component/HttpFoundation/AcceptHeaderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ 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, ';=');
$parts = HeaderUtils::split($itemValue ?? '', ';=');

$part = array_shift($parts);
$attributes = HeaderUtils::combine($parts);
Expand All @@ -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,17 +162,14 @@ 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)
{
if ('q' === $name) {
$this->quality = (float) $value;
} else {
$this->attributes[$name] = (string) $value;
$this->attributes[$name] = $value;
}

return $this;
Expand Down
17 changes: 6 additions & 11 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 $autoLastModified = 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 @@ -317,7 +314,7 @@ public function sendContent()
*
* @throws \LogicException when the content is not null
*/
public function setContent($content)
public function setContent(?string $content)
{
if (null !== $content) {
throw new \LogicException('The content cannot be set on a BinaryFileResponse instance.');
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
5 changes: 1 addition & 4 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
11 changes: 3 additions & 8 deletions src/Symfony/Component/HttpFoundation/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,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 All @@ -102,7 +99,7 @@ public function move($directory, $name = null)
/**
* @return self
*/
protected function getTargetFile($directory, $name = null)
protected function getTargetFile(string $directory, string $name = null)
{
if (!is_dir($directory)) {
if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
Expand All @@ -120,11 +117,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
*/
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/FileBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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 comments on commit 61da067

Please sign in to comment.