Skip to content

Commit

Permalink
Merge pull request #23 from carnage/fix-strict-type-bc
Browse files Browse the repository at this point in the history
Stricter input/output type declaration, matching original type signatures from `2.0.0-beta5` release
  • Loading branch information
Ocramius authored Jun 26, 2021
2 parents 77c248d + f0df7d3 commit 2d6dce9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 36 deletions.
9 changes: 1 addition & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.18.2@19aa905f7c3c7350569999a93c40ae91ae4e1626">
<file src="src/Escaper.php">
<DocblockTypeContradiction occurrences="1">
<code>is_string($encoding)</code>
</DocblockTypeContradiction>
<MixedArgument occurrences="10">
<code>$chr</code>
<code>$chr</code>
Expand Down Expand Up @@ -42,10 +39,7 @@
<code>array&lt;string, array{0: string, 1: string}&gt;</code>
<code>array&lt;string, array{0: string}&gt;</code>
</InvalidReturnType>
<InvalidScalarArgument occurrences="1">
<code>1</code>
</InvalidScalarArgument>
<MissingReturnType occurrences="10">
<MissingReturnType occurrences="9">
<code>testCssEscapingReturnsStringIfContainsOnlyDigits</code>
<code>testCssEscapingReturnsStringIfZeroLength</code>
<code>testHtmlAttributeEscapingEscapesOwaspRecommendedRanges</code>
Expand All @@ -54,7 +48,6 @@
<code>testReturnsEncodingFromGetter</code>
<code>testSettingEncodingToEmptyStringShouldThrowException</code>
<code>testSettingEncodingToInvalidValueShouldThrowException</code>
<code>testSettingEncodingToNonStringShouldThrowException</code>
<code>testUnicodeCodepointConversionToUtf8</code>
</MissingReturnType>
</file>
Expand Down
25 changes: 6 additions & 19 deletions src/Escaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
use function bin2hex;
use function ctype_digit;
use function function_exists;
use function gettype;
use function hexdec;
use function htmlspecialchars;
use function iconv;
use function in_array;
use function is_string;
use function mb_convert_encoding;
use function ord;
use function preg_match;
Expand Down Expand Up @@ -132,17 +130,11 @@ class Escaper
* Constructor: Single parameter allows setting of global encoding for use by
* the current object.
*
* @param string $encoding
* @throws Exception\InvalidArgumentException
*/
public function __construct($encoding = null)
public function __construct(?string $encoding = null)
{
if ($encoding !== null) {
if (! is_string($encoding)) {
throw new Exception\InvalidArgumentException(
static::class . ' constructor parameter must be a string, received ' . gettype($encoding)
);
}
if ($encoding === '') {
throw new Exception\InvalidArgumentException(
static::class . ' constructor parameter does not allow a blank value'
Expand Down Expand Up @@ -183,10 +175,9 @@ public function getEncoding()
* Escape a string for the HTML Body context where there are very few characters
* of special meaning. Internally this will use htmlspecialchars().
*
* @param string $string
* @return string
*/
public function escapeHtml($string)
public function escapeHtml(string $string)
{
return htmlspecialchars($string, $this->htmlSpecialCharsFlags, $this->encoding);
}
Expand All @@ -196,10 +187,9 @@ public function escapeHtml($string)
* to escape that are not covered by htmlspecialchars() to cover cases where an attribute
* might be unquoted or quoted illegally (e.g. backticks are valid quotes for IE).
*
* @param string $string
* @return string
*/
public function escapeHtmlAttr($string)
public function escapeHtmlAttr(string $string)
{
$string = $this->toUtf8($string);
if ($string === '' || ctype_digit($string)) {
Expand All @@ -219,10 +209,9 @@ public function escapeHtmlAttr($string)
* Backslash escaping is not used as it still leaves the escaped character as-is and so
* is not useful in a HTML context.
*
* @param string $string
* @return string
*/
public function escapeJs($string)
public function escapeJs(string $string)
{
$string = $this->toUtf8($string);
if ($string === '' || ctype_digit($string)) {
Expand All @@ -238,10 +227,9 @@ public function escapeJs($string)
* an entire URI - only a subcomponent being inserted. The function is a simple proxy
* to rawurlencode() which now implements RFC 3986 since PHP 5.3 completely.
*
* @param string $string
* @return string
*/
public function escapeUrl($string)
public function escapeUrl(string $string)
{
return rawurlencode($string);
}
Expand All @@ -250,10 +238,9 @@ public function escapeUrl($string)
* Escape a string for the CSS context. CSS escaping can be applied to any string being
* inserted into CSS and escapes everything except alphanumerics.
*
* @param string $string
* @return string
*/
public function escapeCss($string)
public function escapeCss(string $string)
{
$string = $this->toUtf8($string);
if ($string === '' || ctype_digit($string)) {
Expand Down
9 changes: 0 additions & 9 deletions test/EscaperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ protected function setUp(): void
$this->escaper = new Escaper('UTF-8');
}

public function testSettingEncodingToNonStringShouldThrowException()
{
$this->expectExceptionMessage(
"Laminas\Escaper\Escaper constructor parameter must be a string, received integer"
);
$this->expectException(InvalidArgumentException::class);
new Escaper(1);
}

public function testSettingEncodingToEmptyStringShouldThrowException()
{
$this->expectException(InvalidArgumentException::class);
Expand Down

0 comments on commit 2d6dce9

Please sign in to comment.