What are the Slack Archives?
Itβs a history of our time together in the Slack Community! Thereβs a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.
Because this space is not active, you wonβt be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..
When running the code style sniffer upon commit, I'm getting errors like this: ```FOUND 1 ERROR AFFE
When running the code style sniffer upon commit, I'm getting errors like this:
FOUND 1 ERROR AFFECTING 1 LINE ---------------------------------------------------------------------- 173 | ERROR | [x] @throw annotation `\Exception` superfluous and | | needs to be removed ----------------------------------------------------------------------
Indeed, my code might throw an exception at this very place. Thus, having a @throws annotation in the function's docblock should be fully correct. How would the calling function know that it needs to catch it otherwise? Am I missing something here or is the code sniffer behaving wrongly here?
Comments
-
Just a guess, but perhaps itβs because Youβre using a generic Exception class. If Youβd throw βSomeSpecificExceptionβ exception this error would go away.
0 -
Just wanted to give you a most basic example.
It also appears with custom exceptions, unfortunately.0 -
-
Yes, needs to be
throws
as per standards afaik.0 -
My assumption is that the sniffer will complain if the annotated function doesn't explicitly throw this type of exception. If e. g. it is thrown in a nested function, it won't recognize it.
0 -
As far as I remember, this code sniffer rule checks only exceptions, which you explicitly define inside the method body. So if exception is not directly in the method body, annotation is not needed
0 -
Yes, looks like it. But how would you get around this then? Catching and rethrowing the exception as a workaround doesn't feel right.
0 -
https://github.com/spryker/code-sniffer/blob/07022098a526f7d64a04da148c95d86e93360111/Spryker/Sniffs/Commenting/DocBlockThrowsSniff.php#L18
Looks like itβs intentional:We only ever declare them for the exceptions inside the own method.
So you can extend this sniff, or disable it. Or maybe @UQK3ZPJEN can suggest better, since heβs author of this sniff
0 -
yes, we only use primary level exceptions, as nested starts to lie soon (as it is almost impossible to assert by sniffer).
0 -
you can always remove this on project level - or replace it.
0 -
What is the concrete code in your method?
0 -
It's basically this:
/** * @throws \Exception */ protected function foo() { // this will throw an exception that should be propagated to the caller of foo() $this->nestedFoo(); }
0 -
In that case you could only extend/overwrite/silence the core sniffer rule.
I wonder if a bang-overwrite could make sense* @throws \Exception !
with the ! as comment it could ignore the "removal" requirement.
I use a similar approach for IDE helper based annotations to be auto-managed.
0 -
I was hoping for something like that. But doesn't help, unfortunately.
0 -
/** * @param array $tourTime * @param string $localeTransfer * * @throws \Exception ! * * @return string */ protected function getDeliveryDateText(array $tourTime, string $localeTransfer): string { ... }
still gives
---------------------------------------------------------------------- FOUND 1 ERROR AFFECTING 1 LINE ---------------------------------------------------------------------- 174 | ERROR | [x] @throw annotation `\Exception` superfluous and | | needs to be removed ----------------------------------------------------------------------
0 -
"wonder" + "could make sense" => I was only spitballing ideas here, none of this is of course implemented.
0 -
Sure, I appreciate any idea. Didn't expect it to be final, but hey, we could have been lucky.
0 -
I added the idea here for further feedback from everyone: https://github.com/spryker/code-sniffer/issues/275
Also, if someone wants, they can do a PR here to support this.0 -
@U01LKKBK97T Can you check if https://github.com/spryker/code-sniffer/pull/278 would work here as expected?
I am planning a release for end of this week, so this fit in with almost no work needed.0 -
Seems to work, great job!
0
Categories
- All Categories
- 42 Getting Started & Guidelines
- 7 Getting Started in the Community
- 8 Additional Resources
- 7 Community Ideas and Feedback
- 76 Spryker News
- 929 Developer Corner
- 787 Spryker Development
- 89 Spryker Dev Environment
- 362 Spryker Releases
- 3 Oryx frontend framework
- 35 Propel ORM
- 68 Community Projects
- 3 Community Ideation Board
- 30 Hackathon
- 3 PHP Bridge
- 6 Gacela Project
- 26 Job Opportunities
- 3.2K π Slack Archives
- 116 Academy
- 5 Business Users
- 370 Docker
- 551 Slack General
- 2K Help
- 75 Knowledge Sharing
- 6 Random Stuff
- 4 Code Testing
- 32 Product & Business Questions
- 70 Spryker Safari Questions
- 50 Random