Cakephp 3: what are the best pratices to manage translation exceptions in views? - cakephp-3.0

In April '18, phpDocs has been updated for all #throws (good idea) including I18n::getTranslator.
Now all views using __() functions get a warning in the IDE (always normal).
But, what are the best practices to manage that kind of warning in a view ?
It does not seem a good idea to add a try/catch, nor to overload the __() function.
The #throws directive would be interesting but it does not work in a view.
Any idea ?

Related

How to cache "exists" validation in Yii2

I'm trying to cache the db calls for Yii2 exists validation, but can't work out where to initiate it.
Because I'm using a multi-model form with a lot of relations, the overhead is getting a little too much.
Any ideas?
You'd better not. Actually, there is an issue on Yii2 official Github project where one of the framework's core developers, Alexander Makarov aka #samdark, explains why caching ExistValidator is a bad idea:
Exist validation isn't the kind of validation to be cached. Each second database may change its state so it should be validated just before it's saved.
This is not supported by Yii, you either have to :
Extend the ExistValidator and implement your caching logic there
Add a custom ActiveQuery class to your model in question and override
the exists() and count() methods

How to configure yii2 with phpstorm

I have found some ways to config Yii 1 with phpstorm. But I don't know how to deal with Yii 2.
When use phpstorm, there are some errors. such as Yii::$app->db2, This method will cause
"Field access via magic method",
and some method bindValue,queryOne will also cause some problems.
There is, at this point, no native functionality for Yii2 as a framework (It's just Yii1 and Symfony if I recall correctly).
That being said: Yii2's source code is filled with DocBlock's (and the needed #var and #property statements. So the editor is able to make out a lot on its own now even without native support.
For that reason I don't think they will be implementing support for it anytime soon.

Why should I use Assert.IsNull in Sitecore?

I've seen a lot of code in Sitecore where Assert.IsNull is used before any logic;
e.g.
Database database = Factory.GetDatabase(itemUri.DatabaseName);
Assert.IsNotNull(database, itemUri.DatabaseName);
return database.GetItem(attribute);
Could someone help me understand why I would use this?
This topic isn't really specific to Sitecore, even though in this case the assert methods are within the Sitecore library.
In general, assertions are used to ensure your code is correct during development, and exception handling makes sure your code copes in unpredictable circumstances.
Take a look at these SO questions for some very good explanations.
When to use an assertion and when to use an exception
When to use assert() and when to use try catch?
Here's an article specifically about the use of Sitecore assertions:
http://briancaos.wordpress.com/2012/01/20/sitecore-diagnostics-assert-statements/

When/why to use custom exceptions

I read a thread on this topic on this very forum which listed some reasons to use custom exceptions but none of them really seemed strong reasons (can't remember the reasons now).
So why would you use custom exceptins? In particular, I have never understood the decision making process between using a standard or custom exception to indicate a shopping basket is null (I think a custom one is used as an empty collection is not exceptional and this is a business process thing). More clarification is needed, however.
Thanks
Here is my take:
If any of the standard exceptions do not match the exceptional situation, create a custom exception
If there is additional information that you need to pass in to the exception, create a custom exception
If there is meaning to having your own exception class, create a custom exception (that is, other developers will benefit from being able to catch it)
In regards to things like null arguments - I would never use a custom exception. The NullArgumentException (.NET) / IllegalArgumentException (Java) is perfectly satisfactory.
Jared Par has a blog entry about this, here.

.NET Exception Explorer

Does anyone know of a program or plug-in or anything that I can find out what are all the exceptions any method may throw?
I think JAVA has this build in if I'm not mistaken. Where the compiler tells you what exceptions this method will throw.
Does the same exist for .NET?
Thanks
Edit: After searching around more, I wish there was tool like Object Explorer, except for Exceptions. You select the class or method and it lists the exceptions, at that level, which are thrown by the class. The tool links provided are a great start. Thanks!
I don't know if this is exactly what you are looking for, but:
http://www.red-gate.com/Products/Exception_Hunter/index.htm
Note: I've never used the product, and I don't work for Red Gate, I just remember seeing it advertised before.
You can see this information with intellisense in Visual Studio. When you highlight a method name in the intellisense list, its description should contain a list of exceptions at the bottom. This information is added by properly commenting your methods and classes. If you are using a library that is not part of the framework, then you will only get this information if the developers of the library appropriately commented their code.
.NET doesn't require or permit each method to state which exceptions it throws. As I recall, it was felt that this would lead most developers to simply state "throws Exception".