I installed the last version (Oxygen) of Eclipse for PHP. But now there are lots of error annotations that I think they shouldn't be.
Almost all of them have to do with Exception:
throw new Exception('Exception message');
The annotation message in the popup hint is like so:
Exception cannot be resolved to a type
And the hint offers me some quick fixes, which are using the Exception class declared in libraries imported with Composer.
Why is that? As far as I know, Exception is still an internal PHP class (no need to import it). I know I should be using more specific Exception classes, but for now, Exception works for me. And it shouldn't be marked as an error in Eclipse. Prior versions didn't detect this as an error. Furthermore the application runs without any problem.
Is this a bug? Otherwise, how do I disable this type of error annotation?
EDIT:
Another annoying issue happening is that the code assistant is not displaying any php internal function. Only functions, classes and methods declared in my app or in imported libraries. For instance, if I type:
str
the code assistant displays classes from Doctrine, Geocoder, etc, and imported functions like "strip_quotes", but nothing about strstr, str_pad, strpos, etc
Did you forgot about namespaces and PSR-4?
throw new \Exception('Exception message');
So when you are using a class which must be autoloaded, you must declare it via use or call it with full path (with namespace).
If you don`t want to write correct code and this message is annoying you, then I'm pretty sure that you can turn off this message via Eclipse configuration.
Related
I have some php code in a class that uses inheritance and requires and during that loading process, the error handler is setup. This interferes with my phpUnit test as it captures the exceptions/errors that phpUnit needs to run my tests. This is a large project and I can't just go change stuff. I can add stuff, though.
I cannot change the existing error handler code and as such I am looking for a way to make phpUnit be able to prevent the error handlers being called. I thought about overriding the call to set_error_handler, but this as expected gives the "cannot redeclare" error.
Currently I manually edit setup portion of the error handler by commenting the set_error_handler function call, but it would be nice if I could make this automatic without changing the source code (only changing the test code).
Is my problem clearly enough described?
Is what I need possible without adding runtime modules or changing the source code, i.e. just change the unittest php file?
Using php 5.5.
I am using these two libraries from composer
"require": {
"alterfw/php-form-generator": "^0.1.2",
"rlanvin/php-form": "dev-master"
},
Biggest issue is both has the same class name Form with no namespace defined. Now no matter either I am instantiating only one class or both on the same page it is giving me an error as below
Fatal error: Call to undefined method Form::create() in...
create method is to generate form markup from alterfw/php-form-generator library.
So when I tried this
$form = Form::create('path-to-action.php');
$form->add(Form::text('settings')->setLabel('Settings')->setValue('None'));
echo $form->render();
Giving me the error mentioned above. For me it is difficult to understand why even I hven't instantiate another class it is still giving error.
I a also not so much familiar with composer to dig into myself without any guide.
So is there any way so I can use both libraries at the same time?
Tip: Packages without a vendor namespace are a bad practice, as you can see here. I recommend creating an issue for both packages saying they should really add a vendor namespace.
The problem here is that as soon as you use Form in your code, the class isn't already loaded and Composer's autoloader is executed. As it has 2 packages to load the class from, it'll pick the first registered autoload rule. This results in the wrong Form class: Problem!
You can swap the packages position in your composer.json, but this means the other class is no longer usable.
Another solution is to require the class yourself by using the require()/require_once() PHP functions. This is a bad practice and also means you can only use one of the 2 Form classes (requiring the other form class after one is already required results in a "Class with name "Form" already exists" PHP error).
TL;DR: You can only use one of the 2 packages in the same application.
There is really no shortage of form validation libraries. Every single framework should have one component for this.
So I'd recommend selecting your libraries carefully.
We have here the first library "alterfw/php-form-generator", which is 4 years old, being unmaintained since then, until it got forked 4 months ago with adding composer.json. Four years ago, PHP 5.2 was still common, so it is no real surprise that this code has no namespace. It also has no real documentation, because the link in the readme file is dead. The forked repository of this library does not allow creating issues. :( I doubt that the upstream repository will care about anything after 4 years of not maintaining it.
The second library "rlanvin/php-form" seems to be tied to non-public code. You opened a ticket to add namespace to that one class, and effectively got rejected (saying that the maintainer will not add namespaces because it is more convenient for him, but you could edit the file yourself to add it simply does not cut it). Packagist shows that this library has 31 installs. It's not completely right to say that "nobody uses this code", but it is very close.
If your task is form validation and form generation, I'd say that either the Symfony or Zend components will likely perform that task very well. There probably is no need to resort to libraries that "nobody" uses, and that do not apply modern development methods like namespaces (available since PHP 5.3, which was released in 2009).
I have a project containing, amongst others, the following composer.json dependencies:
"propel/propel1": "dev-master"`,
"halleck45/phpmetrics": "dev-master"
I recently did a composer update and found that a new version of a library required by PhpMetrics, called Hoa, introduces a new class \EngineException to emulate a new PHP7 class. Unfortunately Propel 1 also defines \EngineException, and so a conflict results.
The correct fix for this would be to upgrade to Propel 2, which uses namespaces. However this is still in alpha and is subject to BC breaks, so is not really workable for me.
My present fix is to lock Hoa to a specific version that does not have the new class:
"hoa/core": "2.15.04.*"
That's not a bad solution, but it isn't entirely satisfying to lock a library to an old version.
In the Hoa code, the only way for the new class not to be loaded is to be running PHP 7, which is again not feasible. However, it also occurs to me that Hoa only needs to be required when PhpMetrics runs. This is a stand-alone code analysis tool and only sits in the root of the project for convenience; the rest of the project does not use this library.
Thus, it would be great if I could call something in Composer to ask that this class is not (auto)loaded, or perhaps something to do the same in the composer.json. It is being needlessly loaded at present - I don't know whether it is being autoloaded incorrectly or whether it is being required manually by Composer.
It may help to know that Hoa classes have been added by Composer to the auto-generated autoload_psr4.php script. As far as I can understand the docs, this means it is autoloaded, and there is nothing in my project that would require any of the Hoa classes.
Fixed by https://github.com/hoaproject/Core/commit/8ed00fe9345c4f8b2679a256926d6d24994ea842.
The new exception architecture introduced in PHP7 [1] has been totally
redesigned [2]. This patch updates the retro-compatibility classes
according to this new architecture. Consequently, the BaseException
class has been removed, along with EngineException and
ParseException. While these latters could be implemented (not as
is), we prefer to, so far, only implement the Throwable interface.
Let see if we can implement (still for the retro-compatibility) the
Error, TypeError and ParseError class.
[1]: https://wiki.php.net/rfc/engine_exceptions_for_php7
[2]: rfc/throwable-interface
I was curious, so I looked it up. Hoa indeed has a broken approach, having the file Core.php always included, by a "file" autoload in composer which in turn includes Consistency.php. The latter defines your class.
You could raise an issue with the developers at Hoa, to use class_exists to check for the method rather than the currection version check they are using. This could cause the propel autoloader to load its own. Another way would be to define their autoloading correctly, but they prefer to load manually as it seems.
I got hold of a dip into CI from CI forums and websites, which I have working. Basically; I have a fully working system in CodeIgniter, I have run tests that show a standard PHP file on the same server can access the classes and libraries that I made available.
One of the other legacy systems runs on Zend, and throws an exception when it incudes the CodeIgniter files. I can't get past:
$BM =& load_class('Benchmark', 'core');
in the dip file, with a long error being thrown by Zend. It throws (among much other information):
exception 'Zend_Exception' with message 'At least one error occurred including "Benchmark.php"; see includeErrors property'
Pretty much everything I can find on the internet finds ways to use the libraries from CodeIgniter, but that's the reverse of what I'm looking for. If I comment out the Benchmark loading, then it fails on Hooks, which leads me to believe that Zend has usurped or overloaded the load_class() function with its own, and is causing problems that way.
Has anybody gotten them working before?
I can't directly answer the question, but I just stumbled over a bit of rotting code referencing an Exception's "includeErrors" property.
After some search, I can maybe give a bit of context to what you observed.
Behaviour exists in some versions of ZF1 , see http://framework.zend.com/issues/browse/ZF-2463
if Zend_Loader includes a file
and the mere act of including it results in some PHP error (very often that will be E_STRICT if for example some inherited method signature doesnt match, or maybe if file is not found),
then it will throw an exception and add a "includeErrors" property to it
In web frameworks I've built and used in the past, there's been some means to specify some form of "last resort" error handler. I'd appreciate any help in determining how to accomplish that goal using CodeIgniter, which is a legacy part of a product I'm working on.
The goal of the last resort error handler is to capture any exception that's bubbled up, unhandled, from the application logic. Since, at this high framework level, the handler can't resolve the exception, a typical implementation is to log the error (with associated context) and present a user-friendly error page rather than a scary, technical exception page.
I wasn't able to find support in the CodeIgniter documentation, but I expect there must be support for this. Did I not find support because I should use PHP's set_error_handler() and set_exception_handler()? (I'm new to PHP, but expert in Java, Ruby/Rails.)
Thanks in advance for you guidance!
It seems CodeIgniter 2.0+ registers the handlers you specified to load the CI_Exceptions class. I usually put a MY_Exceptions library in the application/libraries folder to "catch" them before CodeIgniter. I'd rather handle them gracefully than let CI show it's error pages.