Interface and interpreted language - php

I'm discovering Php and it's Oop implementation and wondering if there is really an practical utility of using Interfaces when we use a non compiled language and when errors can only be discovered at execution time. Am in missing someting?. Another point is that I didn't find any extension or plugin for editor or Ide which forced implementing the Interface when creating a class which is for me the essential point because Interface are really useful when working with others and functionals requirements. So that make me thinking that we need to know the contracts of the Interfaces before using them..
I didn't find any satisfying reading about these question and hope someone would give me some clues...

As my Questioning were on the way I can be sure of following the contract implied by an Interface I had a look on the differents Ide or extension (I'm using mainly vscode)and now what I understand is I rely on their functionalitie about continuous code validation. For example Dev Sense PHP tools for Vs Code do the job but normal Php Intelliphense doesn't.
If I don't use an Ide or extension with this code validation I will have executing time errors if I fail to execute the whole Interface contract.

Related

How to check if a PHP project works well with a new PHP Version

Is there a good way to test if a PHP project works well with a new version of PHP?
Lets say we have a project developed under PHP 5.0 and want to run it now with PHP 5.4. The project have no unit tests or something like that.
Just run it with PHP 5.4 and click around to see if there are errors is not save enough.
Run your tests. If you don't have tests, write some now under PHP 5.0. Then run them under 5.4. If they break, then you've found something that needs to be fixed. Having a suite of test scripts is good practice anyway so if you haven't got any, this is a good opportunity to start writing them. Look up phpUnit, which is the most common PHP tool for writing unit tests.
For creating a test suite on an existing project, I recommend using a tool like Selenium or Sahi which can record a browser session. Turn on the recording and start testing as normal. Voila: One repeatable test. You're going to have to do this kind of testing anyway, so you may as well record them. Granted, those are functional tests, rather than unit tests, but they are tests all the same, and if you can cover enough of your functionality with them then you'll have a fairly comprehensive demonstration that the system is working.
Syntax check: Use PHP's command-line -l option in a batch job to run a syntax check on all your files. This will prove that everything parses successfully.
That will eliminate the obvious problems.
Use a decent IDE to develop your code in. IDEs like Netbeans will highlight syntax issues and warning for you and underline the relevant code. This makes finding bugs much much easier.
If you're still developing in Notepad, you're missing out on a whole world of good stuff.
If you're using ereg() or related functions, they need to change to preg_match() etc. You can get away with still using mysql_query() for DB access, since that's only deprecated in 5.5, but if you're using it you may as well consider this to be a good time to make that change too.
Look up the Migration Guides provided by PHP. These give full details of all the code-breaking changes between PHP versions. In particular, pay attention to the deprecated features.
The most significant version for this was 5.3: A lot of old code was broken by the features which were deprected in 5.3. These were features like magic_quotes and register_globals; they had been considered bad practice for a very long time before that, but it took them until 5.3 to actually deprecate them. If you're using them, this will be the biggest problem you'll have to face.
Tools like PHPMD and PHPCodeSniffer, PHP Lint may help to analyse your code. They aren't really designed for version compatiblity checking, but may help you find issues.

What is the difference between Rephactor and Scisr PHP refactoring tool?

Rephactor and Scisr both are automated refactor tools for PHP. Both are under development and provides same refactor functions. Can anyone tell me the exact difference in both tools?
I wrote Scisr, so perhaps I can offer some insight. The short answer is that they're simply different tools. They each have a subset of functionality, and you may find that both, one, or neither offer the exact functionality you wish for. Each one offers the set of features that the developer got around to implementing - i.e. somewhat incomplete.
The longer (and slightly more biased) answer is that they are philosophically different. Luckily, both tools offer little introductory blurbs that should give you a sense of what each developer set out to create. Here is Rephactor's and here is Scisr's.
I wrote Scisr after trying all the PHP refactoring tools I could find (including rephactor) and being dissatisfied with all of them. rephactor was definitely the most useful of all the tools I tried, but I thought it made too many demands - it wanted to hook into SVN, and testing tools, and all sorts of other stuff. These were all good processes to be following, but the need to integrate meant I couldn't pick the tool up and immediately use it on my project. So I set out to write a simpler, single-purpose tool, and Scisr was born.
IIRC I also looked at rephactor's source code and found it using regular expressions and other solutions I considered inadequate. I could be misremembering, though, so please forgive any untruth in that statement.
One other consideration should be product health. Unfortunately, both projects are more-or-less inactive. Scisr is more recently updated, and I'm still accepting pull requests, but I'm not actively working on it any more. Rephactor was last updated in 2009.
2019+ answer
Looks like both mentioned packages are dead since 2013. Yet the need to instant refactoring is still here, so I'd like to share 2019 alternative that is in active development with 200 downloads/day.
In 2017 I started a small project called Rector: https://github.com/rectorphp/rector
Rector can help you with anything that you do manually
Just out the box it supports:
Rename classes, methods, properties, namespaces, constants... anything :)
Add, replace or remove arguments
Add parameter or return type declarations without docblocks - just with static analysis
Upgrade from PHP 5.3 to PHP 7.4
Migrate from one PHP framework to another
Complete PHP 7.4 property type declarations
Turn Laravel static to Dependency Injection
...
It's a CLI tool, that is easy to use:
composer require rector/rector --dev
vendor/bin/rector process src --level code-quality
Read more in README

PHP code validation when refactoring

As a developer that's used to static typing I usually let the compiler tell me if the code is correct, logic flaws excluded of course. But when refactoring PHP I find it is VERY hard to know that my code is correct.
There always seem to be some lingering reference to some method or member somewhere that get's broken and doesn't show up until runtime. Even when using the limited refactoring support in Zend Studio things tend to get broken somehow.
Due to the dynamic nature of the language itself, I understand it is a hard problem. But are there any tools out there to statically verify PHP code so that I know that it is okay before runtime? I don't want to see any more "Undefined property" error messages.
Write tests for your code (http://www.phpunit.de/), setup a continuous integration server, run UI tests (http://pear.php.net/package/Testing_Selenium/). With enough testing, you will find your problems straight after you commit bad code. Just keep the test code coverage high.
That's one of the main points of unit tests - you can refactor your code without actually breaking it, because you check all functionality after every change.
I would recommend #viraptor's solution for preventing your current problems.
Here is some information on Static Analysis and Refactoring in PHP.
Static Analysis
http://strategoxt.org/PHP/PhpSat
PHP Refactoring
Tools for PHP code refactoring
unit tests. i'm the author of Testilence

What tool can I use to generate a PHP class usage report for my application?

I have a fairly large object-oriented php 5 project, and as part of a change impact analysis, I'd like to compile a report on the usage of each existing class throughout the project.
It would help me immensely if I could find an existing tool that will analyze all the files in my project and generate some sort of report that lists, for example, all the class names of objects instantiated for each class in the project, and allow me to at least search this easily and quickly.
Any help here would be appreciated!
Check out nWire for PHP. It analyzes your code and recognizes such associations. It is built as an interactive tool, not as a reporting tool, but, if you insist, you can still connect to its' database (it uses H2, which is SQL compatible) and use an external reporting tool.
IMO Zend has some profiling tools that do just that, Or you can extrapolate this information from their Accelerator log.
Or try this with XDEBUG
Xdebug can trace your code and create code coverage statistics. There are additional tools like Spike PHPCoverage, which can generate nicely formatted reports, but since these are intended for test-coverage, it'll just give you a boolean result (eg. line of code is used or not used). You probably want a more detailed view (eg. how many times is it used).
Another option is to use the function trace feature of Xdebug. This will give you a detailed report of the actual call graph. You can determine which files was used the most from this. You'll need to write a parser for the data manually, but that shouldn't be too hard.
Finally, you could do the same thing with a static call graph. There are some tools available for php. Here are a few:
http://www.doxygen.nl/
http://phpcallgraph.sourceforge.net/
http://www.bytekit.org/
Again, you probably need to do some additional manual parsing on the output from those tools, to get something that applies to your use case.
The clever guys at Particletree, the same people behind the functionally and aesthetically gorgeous Wufoo often publish and release their PHP toolsets and utilities, the most recent of which being their PHP Quick Profiler. As you can probably tell, I have a huge amount of respect for those guys and love the stuff that they do.
A good PHP profiler is often hard come by, and PQP is most certainly the best I've come across. That said, nearly all of the various application frameworks have some form of profiling system, humble or otherwise, but none as nearly as in-depth and helpful as PQP. However, I usually find that the framework profiling tools are more linked into the code automatically, and if you use the framework's standard libraries then you'll have to do a lot less implementation with the profiling tool (this is definitely the case with CodeIgniter). But if you want that extra bit of power and flexiblity, PQP is great.
Let me know if you find anything better - I'd love to see it!
Jamie

PHP Lexical and Syntax analyzer written in C/C++

I'm writing a PHP IDE in Qt4 for my Master's project. I am trying to duplicate a lot of the functionality of Eclipse or Visual Studio without all the bloat and the overhead. I'm trying to do some code completion but I need to do some syntax analyzing to have intelligent code completion. I've looked at the PHP source code and thought about just compiling the Zend engine in my code (and give credit where credit is due), but even after staring at the code for a couple of days I'm not sure where to start with that.
Does anyone know some C++ code that already accomplishes this, or have used the Zend engine for their own purposes, or have some advice about writing my own?
PHP does not have anything even remotely close to a formal LALR(1) or LL(k) grammar that you could use to implement this in a manner suitable for academia. Zend's parser is not context-aware and is full of ad-hoc disambiguations from what I've seen.
That is not to say you cannot write something that does simple syntax highlighting. Take for example this javascript implementation. The code for their tokenizer is here and an active thread about the project is here.

Categories