Skip to content

Commit

Permalink
Use declare strict_types
Browse files Browse the repository at this point in the history
  • Loading branch information
gapple authored Jan 3, 2024
1 parent b95700f commit 526c256
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/Bytes.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class Bytes
Expand Down
2 changes: 2 additions & 0 deletions src/Date.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class Date
Expand Down
2 changes: 2 additions & 0 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/DisplayString.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class DisplayString
Expand Down
2 changes: 2 additions & 0 deletions src/InnerList.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class InnerList implements TupleInterface
Expand Down
2 changes: 2 additions & 0 deletions src/Item.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class Item implements TupleInterface
Expand Down
2 changes: 2 additions & 0 deletions src/OuterList.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Parameters.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/ParseException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class ParseException extends \RuntimeException
Expand Down
14 changes: 8 additions & 6 deletions src/Parser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class Parser
Expand Down Expand Up @@ -115,7 +117,7 @@ private static function parseInnerList(string &$string): InnerList
/**
* @param string $string
*
* @return \gapple\StructuredFields\Item
* @return Item
* A [value, parameters] tuple.
*/
public static function parseItem(string $string): Item
Expand All @@ -137,7 +139,7 @@ public static function parseItem(string $string): Item
*
* @param string $string
*
* @return \gapple\StructuredFields\Item
* @return Item
* A [value, parameters] tuple.
*/
private static function doParseItem(string &$string): Item
Expand All @@ -151,7 +153,7 @@ private static function doParseItem(string &$string): Item
/**
* @param string $string
*
* @return bool|float|int|string|\gapple\StructuredFields\Bytes|\gapple\StructuredFields\Token|\gapple\StructuredFields\Date
* @return bool|float|int|string|Bytes|Date|DisplayString|Token
*/
private static function parseBareItem(string &$string)
{
Expand Down Expand Up @@ -180,7 +182,7 @@ private static function parseBareItem(string &$string)
return $value;
}

private static function parseParameters(string &$string): object
private static function parseParameters(string &$string): Parameters
{
$parameters = new Parameters();

Expand Down Expand Up @@ -310,7 +312,7 @@ private static function parseDisplayString(string &$string): DisplayString
$display_string = new DisplayString(rawurldecode($encoded_string));
// An invalid UTF-8 subject will cause the preg_* function to match nothing.
// @see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
if (!preg_match('/^\X*$/u', $display_string)) {
if (!preg_match('/^\X*$/u', (string) $display_string)) {
throw new ParseException("Invalid byte sequence in display string");
}
return $display_string;
Expand Down Expand Up @@ -344,7 +346,7 @@ private static function parseToken(string &$string): Token
*
* @param string $string
*
* @return \gapple\StructuredFields\Bytes
* @return Bytes
*/
private static function parseByteSequence(string &$string): Bytes
{
Expand Down
2 changes: 2 additions & 0 deletions src/SerializeException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class SerializeException extends \RuntimeException
Expand Down
10 changes: 6 additions & 4 deletions src/Serializer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class Serializer
Expand Down Expand Up @@ -218,7 +220,7 @@ private static function serializeDisplayString(DisplayString $value): string
return strtolower(rawurlencode($matches[0]));
};

return '%"' . preg_replace_callback($encode_pattern, $encode_callback, $value) . '"';
return '%"' . preg_replace_callback($encode_pattern, $encode_callback, (string) $value) . '"';
}

private static function serializeToken(Token $value): string
Expand All @@ -228,16 +230,16 @@ private static function serializeToken(Token $value): string
// @see https://tools.ietf.org/html/rfc7230#section-3.2.6
$tchar = preg_quote("!#$%&'*+-.^_`|~");

if (!preg_match('/^((?:\*|[a-z])[a-z0-9:\/' . $tchar . ']*)$/i', $value)) {
if (!preg_match('/^((?:\*|[a-z])[a-z0-9:\/' . $tchar . ']*)$/i', (string) $value)) {
throw new SerializeException('Invalid characters in token');
}

return $value;
return (string) $value;
}

private static function serializeByteSequence(Bytes $value): string
{
return ':' . base64_encode($value) . ':';
return ':' . base64_encode((string) $value) . ':';
}

private static function serializeParameters(object $value): string
Expand Down
2 changes: 2 additions & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

class Token
Expand Down
2 changes: 2 additions & 0 deletions src/TupleInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/TupleTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace gapple\StructuredFields;

trait TupleTrait
Expand Down

0 comments on commit 526c256

Please sign in to comment.