Search code examples
phpmagentoxpathmagento2phpcs

How to remove silencing errors is descouraged error from magento 2?


I have written code for SSO in magento 2.3 and for handling response from differnt IDPs like okta, keycloak I have done following code:

 if(!(@$xpath->query('/saml2p:Response',$xml))) {

            $status = SAML2Utilities::xpQuery($xml, './samlp:Status/samlp:StatusCode');   
}
        else{

           $status = SAML2Utilities::xpQuery($xml, './saml2p:Status/saml2p:StatusCode');   
}

My code is working fine, I can login through different IDPs, but when I checked php coding standard for magento 2 I am getting following error: 'Silencing errors is discouraged; found: @$xpath->query... ' How to resolve this?


Solution

  • In PHP errors can be silenced by using @ operator. On your code you have:

    if(!(@$xpath->query('/saml2p:Response',$xml))) {
    

    so you have a @ before $xpath->query. If you remove @ the error will not be displayed when you will check for coding standards.

    For more information regarding hiding errors you can check the PHP Documentation

    UPDATE:

    Removing @ may cause problems as if an error happens it will stop code execution. You either need to handle the errors with a try catch or make check such as check if variable is null, empty etc.