Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cell::setValueExplicit raises "Unhandled Exception" warning #3754

Closed
8 tasks
PrOF-kk opened this issue Sep 29, 2023 · 9 comments · Fixed by #3765
Closed
8 tasks

Cell::setValueExplicit raises "Unhandled Exception" warning #3754

PrOF-kk opened this issue Sep 29, 2023 · 9 comments · Fixed by #3765

Comments

@PrOF-kk
Copy link

PrOF-kk commented Sep 29, 2023

This is:

- [x] a bug report
- [ ] a feature request
- [x] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

setValueExplicit raises an exception like LogicException or similar, which are considered unchecked by most IDEs

What is the current behavior?

IDE (PhpStorm 2023.2.2) warns about "Unhandled \PhpOffice\PhpSpreadsheet\Exception"

What are the steps to reproduce?

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php

require __DIR__ . '/vendor/autoload.php';

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$spreadsheet->getActiveSheet()->getCell('A1')->setValueExplicit(42, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC);

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

Yes

Which versions of PhpSpreadsheet and PHP are affected?

Using 1.29.0

@oleibman
Copy link
Collaborator

Not a PhpStorm user, so you'll have to do some detective work for me. First, I'll point out that using setValueExplicit here is probably unnecessary if you are using the default value binder - you could more easily use:

setValue(42);

I'll get back to your original statement in a moment. If you use setValue, does the IDE accept that statement without warning?

If it does, the difference between setValue and setValueExplicit regarding Exceptions is that the setValue doc block contains an @throws annotation and setValueExplicit does not. If you add that annotation to setValueExplicit's doc block and restore your original statement, does the IDE now accept it without warning? Is that true regardless of whether your statement is found in a try/catch block (I can't tell if that's the case from your snippet)?

@PrOF-kk
Copy link
Author

PrOF-kk commented Oct 2, 2023

Hello, thanks for taking a look.

setValue(42);

This is in the context of a custom (simple) value binder, so I can't use setValue()

Does the IDE accept that statement without warning?

No, that raises the same warning.


As far as I understand it, PhpStorm doesn't trust the doc block if there's no @throws, and does some code analysis on its own (https://blog.jetbrains.com/phpstorm/2018/04/configurable-unchecked-exceptions/).
So if I manually modify Cell::setValue and lie in the doc block by replacing @throws Exception with @throws \InvalidArgumentException the warning is not raised anymore, so I think the problem is that PhpSpreadsheet\Exception is considered a checked exception instead of an unchecked one.

@oleibman
Copy link
Collaborator

oleibman commented Oct 2, 2023

From the link to which you pointed, it seems like you can configure your own unchecked exceptions list. Can you not configure it to treat \PhpOffice\PhpSpreadsheet\Exception as unchecked?

If you can't, I think you need to check with PhpStorm about what the "correct" solution would be here. In principle, I don't mind adding or changing the @throws annotation, but I'm not willing to declare it as something which is clearly not true.

@PrOF-kk
Copy link
Author

PrOF-kk commented Oct 3, 2023

Can you not configure it to treat \PhpOffice\PhpSpreadsheet\Exception as unchecked?

I could, but then the IDE wouldn't warn me about any \PhpOffice\PhpSpreadsheet\Exception, even where enforcing a try-catch makes sense.

In principle, I don't mind adding or changing the @throws annotation, but I'm not willing to declare it as something which is clearly not true.

Of course, I agree.

I think using InvalidArgumentException or extending it (or LogicException) might be a clean solution

@oleibman
Copy link
Collaborator

oleibman commented Oct 3, 2023

PhpOffice\PhpSpreadsheet\Exception covers more than invalid arguments, and LogicException seems misleading. If you changePhpOffice\PhpSpreadsheet\Exception to extend RuntimeException rather than Exception, would that help? Would it require you to wrap all setValueExplicit and setValue calls in try-catch (which I would definitely consider a problem)? Please note that I will probably not be able to see your reply for a couple of weeks after today.

@PrOF-kk
Copy link
Author

PrOF-kk commented Oct 4, 2023

PhpOffice\PhpSpreadsheet\Exception covers more than invalid arguments

Yes, it's used as a catch-all generic "Exception from PhpSpreadsheet" as far as I can tell

If you changePhpOffice\PhpSpreadsheet\Exception to extend RuntimeException rather than Exception, would that help?

It doesn't raise any warnings from the IDE anymore

Would it require you to wrap all setValueExplicit and setValue calls in try-catch (which I would definitely consider a problem)?

No.
Basically the distinction between checked and unchecked exceptions is a convention from the Java world.

Please note that I will probably not be able to see your reply for a couple of weeks after today.

No worries!

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Oct 16, 2023
Fix PHPOffice#3754. Because `PhpSpreadsheetException` extends `Exception`, PhpStorm warns that calls to `setValueExplicit` and `setValue`, among others, do not handle the exception, because PhpStorm treats `Exception`, by default, as "checked". On the other hand, PhpStorm treats `RunTimeException` as "unchecked" so won't flag such calls. It is reasonable to let `PhpSpreadsheetException` extend `RuntTimeException`, and will eliminate the problem, without having to wrap code in all-but-useless try-catch blocks (e.g. for `setValue`, the code would raise an exception only if you try to set the cell's value to an unstringable object).
@oleibman
Copy link
Collaborator

@PrOF-kk Because I am not a PhpStorm user, please verify that PR #3765 meets your needs and doesn't cause any new problems. I will merge it when you confirm.

@PrOF-kk
Copy link
Author

PrOF-kk commented Oct 27, 2023

Whoops, I didn't get a notification, sorry. I'll get to it as soon as I can, probably Monday

@PrOF-kk
Copy link
Author

PrOF-kk commented Nov 7, 2023

I finally got composer to install the right branch and tested it. No more warnings, thanks!

oleibman added a commit that referenced this issue Nov 7, 2023
Fix #3754. Because `PhpSpreadsheetException` extends `Exception`, PhpStorm warns that calls to `setValueExplicit` and `setValue`, among others, do not handle the exception, because PhpStorm treats `Exception`, by default, as "checked". On the other hand, PhpStorm treats `RunTimeException` as "unchecked" so won't flag such calls. It is reasonable to let `PhpSpreadsheetException` extend `RuntTimeException`, and will eliminate the problem, without having to wrap code in all-but-useless try-catch blocks (e.g. for `setValue`, the code would raise an exception only if you try to set the cell's value to an unstringable object).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants