1

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?

Mansi
  • 25
  • 9

1 Answers1

1

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.

Zenel Rrushi
  • 2,346
  • 1
  • 18
  • 34
  • Thanks for answering. Error does disappear if I remove @, but plugin stops working because error stops the flow, how to resolve that? – Mansi Nov 05 '19 at 10:11
  • You need to handle errors manually. You can wrap it in a try catch, check if `$xpath` is not null etc. – Zenel Rrushi Nov 05 '19 at 10:42