We are looking for a PHP linter, and we are chasing a particular problem that's causing E_NOTICE's, a lot of them:
if($undef_variable)...
if($assoc['undef_key'])...
$undef_variable?...:...
$assoc['undef_key']?...:...
Functionally, the code works perfectly so if the tools was also able to replace on-the-fly such occurences with i.e.
if($undef_variable??null)
That would be a huge help.
Some of the code is in templates that are in included with some pre-set variables (always the same). So ideally the tool would also allow configuring some available global-namespace variables.
The tool should absolutely understand PHP7 syntax, especially anonymous functions.
At a minimum, we need to generate a list of every occurence where a variable is used as a boolean condition and is not defined in the same scope, and every occurence where an array key is used as a boolean condition.
Phpcs- PhpCodeSniffer can be used for that. You need to configure a rule for that. Find it here - Phpcs
Check this rule -
https://github.com/sirbrillig/phpcs-variable-analysis
If you use PhpStorm, it has an option in Inspections. https://www.jetbrains.com/help/phpstorm/php-undefined-variable.html
Related
I want to know how this code is working:
memberpage.php?action=admin_mail_list&type=outbox
Yes, memberpage.php is a page but is admin_mail_list&type=outbox a separate page?
If No, what is it then?
If Yes, why is there no file type after the name (I mean .php or .html)?
That link is using the GET method, meaning variables are defined in the URL rather than the PHP code itself.
For example, if you were to run a Google or Bing search, it wouldn't just be:
https://google.com/search
It would be something like:
https://www.google.co.uk/search?q=test
The benefit of using this, is if the page is refreshed or sent to a friend, the variable won't need to be redefined like POST, it's already defined in the URL.
So, for example, you may have :
http://example.com/example?q=test
The /example page would have this PHP code:
echo $_GET['q'];
which would print "test".
See the following pages if you need more help.
http://php.net/manual/en/reserved.variables.get.php
https://www.tutorialspoint.com/php/php_get_post.htm
You're describing two different parts of a URI. This isn't exclusive to PHP, the URI recommendation applies to all websites regardless of their programming language.
The first (memberpage.php) is the path, and W3 describes it like this:
Path
The rest of the URI follows the colon in a format depending on the
scheme. The path is interpreted in a manner dependent on the protocol
being used. However, when it contains slashes, these must imply a
hierarchical structure.
and the second (?admin_mail_list&type=outbox) is the query string and is described like this:
Query strings
The question mark ("?", ASCII 3F hex) is used to delimit the boundary
between the URI of a queryable object, and a set of words used to
express a query on that object. When this form is used, the combined
URI stands for the object which results from the query being applied
to the original object.
Within the query string, the plus sign is
reserved as shorthand notation for a space. Therefore, real plus signs
must be encoded. This method was used to make query URIs easier to
pass in systems which did not allow spaces.
The query string represents some operation applied to the object, but
this specification gives no common syntax or semantics for it. In
practice the syntax and sematics may depend on the scheme and may even
on the base URI.
To put it simply, the path of the URI dictates which script is to be run, and fields in the query string are parameters to use in that script.
If you're familiar with working on the command line it might be easier to think of these parameters like options on a command line utility. A comparable command might look something like this:
$ php memberpage.php --admin_mail_list --type=outbox
It's important to remember that parameters like this aren't necessarily required to access the URI so it's inappropriate to think of these are arguments on a command line. If your script absolutely needs these parameters to function, you must create that logic within the script yourself, as it is not enforced by the URI.
To answer your question directly:
Yes!
Passing different parameters to the URI can lead to wildly different pages.You absolutely should consider different URI's to be different pages because from the perspective of your users and the larger web, they certainly are. Both users and search engines will consider them distinct and so should you.
That means that the "memberpage.php" script takes two parameters via $_GET:
"action" which has the value "admin_mail_list"
"type" which has the value "outbox"
See: http://php.net/manual/en/reserved.variables.get.php
My question is about the PHP functions for manipulating array elements, like array_pop() and array_shift().
On all examples I've seen (including php.net), since those functions return the value being removed, they are assigned to a variable when executed, for example:
$exampleArray=array("1","2","3");
$removedNum=array_pop($exampleArray);
What I can't find is whether you have to assign the removed value or could you just pop the value from the end and be done with it, like in Ruby, for example.
I have tried and it works, e.g.:
array_pop($exampleArray);
but I'm not sure if this is an acceptable practice in PHP programming? Or should I always assign the value to a variable?
It is valid to use array_pop() and array_shift() to remove unwanted values, and in some cases, can even make sense depending on the data that you're working with.
I.e., if you are working with CSV files, and have an array of lines from that file, where the first line is header data that you know will never change (a bold assumption), and that does not matter to your script, you can safely remove that first line from your array before starting the loop to process the values.
As for whether that's a good practice or not, that's something to discuss with the people maintaining your code...
You can just:
array_pop($exampleArray);
You don't have to assign the value to a variable if you don't need to use it.
It depends on what your application is doing. For example if your application really needs to save the last number being popped then yes. Otherwise, you don't need the variable. Furthermore, It takes memory to create a variable. It may not seem as much for small operations but if you have a loop of a billion operations, then this becomes wasteful. As long as you code is readable, you'll be fine :).
I am using Sublime Text 3 as a PHP editor, I've been customizing the PHP.tmLanguage file to include more syntax scopes, right now I cannot figure out how to capture class method invocations.
$html = new HTML("hr");
$html->output_ml("moo");
output_ml is currently declared as scope variable.other.property.php
I would like to add a scope to pertain to specifically to class method calls, but I am having trouble defining the regex in the tmLanguage file.
I've tried
<dict>
<key>match</key>
<string>(?i)\$[a-z_][a-z0-9_]*->([a-z_][a-z_0-9]*)\s*\(</string>
<key>name</key>
<string>meta.method-call.php</string>
</dict>
You were pretty close, I just tweaked a few things:
(?i:(?!\$[a-z_][a-z0-9_]*->)([a-z_][a-z_0-9]*)\s*\()
First thing I did was wrap the entire expression in the "case-insensitive" modifier, just so nothing got missed. Probably not necessary, but whatever. Second, and more importantly, I took your first group that was outside of parentheses, and made them a negative lookahead. This way, they match the class name and the arrow, but don't report it - basically saying "match whatever is ahead of us as long as we exist in front of it, but don't match us."
Now, you'll have a scope that matches just the method's name.
Demo
Now, here's what I don't get - why you should have to do this. I maintain both a language syntax and a color scheme, so I have a lot of experience with scoping in Sublime Text and TextMate. And to my knowledge, the PHP .tmLanguage already includes scopes for function calls. I'm not a PHP expert by any means, but I know the basics, so I made three examples of different PHP function calls:
Green highlights a large variety of function calls in different languages. In the top example, baz_quux is scoped as meta.function-call.object.php, in the middle example it's meta.function-call.static.php, and in the bottom it's just meta.function-call.php. I don't understand where you're getting variable.other.property.php from.
However, if I take away the parentheses after the function calls above:
I get the following scopes, from top down: variable.other.property.php (aha!), constant.other.class.php, and constant.other.php. If I put the parens back, and add a space or three after the end of the function name, they are still highlighted as functions, in green.
So, while we had some fun with regexes today, ultimately your work has already been done for you. If you're going to be doing more scope-hunting, I highly recommend the ScopeAlways plugin, which, like its name implies, always lists the full scope of the current cursor position in the bottom bar (you can turn it off via the Command Palette if you want). If I get a request to expand my color scheme's highlighting to a new language, I just open up as much code as I can find and poke around with my mouse, looking at the different scopes, then editing my theme to correct colors as needed, or add new ones for brand-new scopes. I then scan through the .tmLanguage file (after converting it to YAML) and see if I missed anything.
Good luck with your work!
I wanted ST3 to include the "$" when selecting PHP files so I edited word_separators by removing $ as such :
"word_separators": "./\\()\"':,.;<>~!##%^&*|+=[]{}`~?"
And now it highlight the whole varibale including the "$" when double clicking a variable. However, now Sublime is not matching/outlining those variables using the default match_selection functionality.
Any thoughts on this? Is there a setting I am missing. Much appreciated.
Please see my answer to this question for an in-depth look at the effect of changing word_separators and various find and select options. Long story short, altering word_separators changes the behavior of double-clicking, as you noted, but other operations, including match_selection, rely on an internal word separators list that apparently can't be altered by the user. So, when you double-click to select a PHP variable, you'll get the $ selected, but other instances of the variable won't be highlighted. They will be selected when using CtrlD to make multiple selections, however:
Additionally, when using the Find dialog, other instances will be highlighted:
So, to address your original problem, if you need to see all the instances of the currently-selected variable, set "find_selected_text": true in your user preferences, double-click to select your variable of interest, then hit CtrlF (⌘F on OS X) to view all occurrences. Just make sure you don't have the regex search button selected, as $ has a special meaning in regexes.
My PHPEd suddenly stopped showing arguments and arg order in the hints, and now just shows a basic description of the function.
Before I go digging around in the config files, has anyone else had this problem?
Thanks!
Edit:
Sorry, I may not have been entirely clear on this. There is no problem with my own classes, only with the actual php functions.
Example:
How it used to work:
I type a PHP function, say strpos. As soon as I type the '(' at the end of it, I get the little yellow box, showing something like this:
int strpos ( string $haystack , mixed $needle [, int $offset=0 ] )
with the first argument bold. If I type it, and then a comma, it bolds the second arg, and so on. This is really nice, since PHP functions are a bit scrambled as far as argument order, and I don't have to look them up every time.
How it works now:
I type a php function, say strpos. As soon as I type the '(' at the end of it, I get the little yellow box. It says something like "strpos - Returns the numeric position of the first occurrence of needle in the haystack string."
There are no arguments shown, which makes the little box basically worthless - I know what strpos does, I just want a reminder of the argument order.
I think this may be a problem with the included PHPDoc, which I never use, but may be the source of the data for the hint box.
I did recently upgrade to 5.6, but ended up removing it and restoring 5.2. I installed to a different folder, and uninstalled from there, but it may have overwritten something in the original folder?
I'm using v5.2 (5220).
A complete re-install seems to have done fixed the problem. Perhaps it was a problem with a partial upgrade or a version mismatch on the settings.
What version of PhpEd are you using ? And did you made an update of PhpEd recently ?
As mentionned in the forum, that can modify the "Function Arguments Tooltip" feature behavior.
One way to check if this is linked to settings is to move the phped.cfgconfig file (save it), and let PhpEd recreates it with default values. If that does restore the tooltips, use a diff program to check what option may have been changed.
Do you see no hints for all classes, or only your own Php classes (in the latter case, this thread has some setting advices) ?
Note: the reason I ask for the version is because of this very recent thread, about PhpEd5.5:
In that case, the advice is to copy %PROGRAMFILES%\nusphere\phped\config\func.rel into %APPDATA%\nusphere\phped\config\ directory overriding the file in it.