1

When I run PHP Code sniffer on my code, I see the below warning.

WARNING | The use of function sleep() is discouraged (Magento2.Functions.DiscouragedFunction.Discouraged)

Which class method provides an alternative for the sleep() method in Magento 2 framework that fixes the above warning and follows the magento best practice.

2
  • Have you found solution? I'm facing same issue. Thanks.
    – Raj
    May 14 '20 at 6:34
  • I found a temporary solution. I answered it here. Please check.
    – nikin
    May 14 '20 at 6:52
2

I have found solution. We have to use usleep function which is working same like sleep function.

Only difference is that usleep function works in microseconds so you have to covert second into microsecond and it's work like a charm.

For example:-

sleep(2); //2 seconds delay here
usleep(2000000);  //2000000 microseconds = 2 seconds
1
  • Hahahah this is Magento !!! In a couple of days they'll add usleep as well hahaha May 27 at 1:12
1

Modify the code from

sleep(<time>);

to

// phpcs:ignore Magento2.Functions.DiscouragedFunction
sleep(<time>);

This is just a temporary solution where in the codesniffer ignores the evaluation of the discouraged function when prepended with the comment phpcs:ignore Magento2.Functions.DiscouragedFunction

1
  • It is just ignore the code during codesniffer. Try solution given by me. Hope it works for you. Thanks.
    – Raj
    May 14 '20 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.