Skip to content

[False positive] Method parameter typing added #63

Closed
@ihor-sviziev

Description

@ihor-sviziev

Hi,

This is a follow-up on the magento/magento2#33353 (comment).

While working on magento/magento2#33353, there were added following changes:

diff --git a/lib/internal/Magento/Framework/Escaper.php b/lib/internal/Magento/Framework/Escaper.php
index dae830dd889d..cb23deb0be4a 100644
--- a/lib/internal/Magento/Framework/Escaper.php
+++ b/lib/internal/Magento/Framework/Escaper.php
@@ -289,7 +289,7 @@ public function escapeUrl($string)
      * @return string
      * @since 101.0.0
      */
-    public function encodeUrlParam($string)
+    public function encodeUrlParam(string $string)
     {
         return $this->getEscaper()->escapeUrl($string);
     }
@@ -328,7 +328,7 @@ function ($matches) {
      * @return string
      * @since 101.0.0
      */
-    public function escapeCss($string)
+    public function escapeCss(string $string)
     {
         return $this->getEscaper()->escapeCss($string);
     }

The SVC failure was failing:

Level Target/Location Code/Reason
MAJOR Magento\Framework\Escaper::encodeUrlParam/lib/internal/Magento/Framework/Escaper.php:292 V085 [public] Method parameter typing added.
MAJOR Magento\Framework\Escaper::escapeCss/lib/internal/Magento/Framework/Escaper.php:331 V085 [public] Method parameter typing added.

image

Basically, adding argument type shouldn't introduce any breaking changes since PHP 7.2 (thanks to https://wiki.php.net/rfc/parameter-no-type-variance).

Examples:

Here are two examples that works fine

  1. w/o strict types, pass string https://3v4l.org/IBGCK
<?php

class A
{
    public function encodeUrlParam(string $string)
    {
        echo $string;    
    }
}

class extendedA extends A
{
    public function encodeUrlParam($string)
    {
        echo $string;
    }
}

$a = new extendedA();
$a->encodeUrlParam('test');

image

  1. with strict types, pass string https://3v4l.org/eFu0n
<?php

declare(strict_types=1);

class A
{
    public function encodeUrlParam(string $string)
    {
        echo $string;    
    }
}

class extendedA extends A
{
    public function encodeUrlParam($string)
    {
        echo $string;
    }
}

$a = new extendedA();
$a->encodeUrlParam('test');

image

  1. w/o strict types, pass int https://3v4l.org/KDom7
<?php

class A
{
    public function encodeUrlParam(string $string)
    {
        echo $string;    
    }
}

class extendedA extends A
{
    public function encodeUrlParam($string)
    {
        echo $string;
    }
}

$a = new extendedA();
$a->encodeUrlParam(1);

image

  1. with strict types, pass int https://3v4l.org/cGn61
<?php

declare(strict_types=1);

class A
{
    public function encodeUrlParam(string $string)
    {
        echo $string;    
    }
}

class extendedA extends A
{
    public function encodeUrlParam($string)
    {
        echo $string;
    }
}

$a = new extendedA();
$a->encodeUrlParam(1);

image

❗ ❌ Note: it doesn't work like that for return types https://3v4l.org/ti9uU

<?php

class A
{
    public function encodeUrlParam(string $string): string
    {
        echo $string;    
    }
}

class extendedA extends A
{
    public function encodeUrlParam($string)
    {
        echo $string;
    }
}

$a = new extendedA();
$a->encodeUrlParam('test');

image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions