PhpStorm live template expression to convert current variable - php

So we can use expressions to transform other variables in live templates.
For example:
Is it possible to apply snakeCase to NAME directly? So whatever I type, gets converted into snake case? Desired result:
Tried snakeCase(NAME), snakeCase(String) and snakeCase(). None seemed to work. Maybe someone had it figured out?

No, it's not possible - you can't pass a variable to itself, it has to be either another live template variable (defined before) or some known value calculated based on clipboard content (snakeCase(clipboard())), file name (snakeCase(fileName())), name got from completion, etc.
If you like to change names of existing variables, you can try String Manipulation plugin, for example

Related

Advanced Parser Subclass No value - Php

I am using Advanced Parser I made this code that extracts the value of the class "text-brand"
However, I want to make the search more exact. Like this but it doesn't work.
"//*[#class='text-brand']", //This way it works but it's not so exact
How can I make it detect the subclass?
"//*[#class='product-price text-brand']",
In this 2nd code I say that the main class is this
"product-price" and that "text-brand" is inside it.
What am I doing wrong?
As an additional note I am using the famous variable $this->xpathScalar($paths)
Solved - Valid answer:
//div[#class='product-price']/ins/span[#class='text-brand']

Why is type checking in PHP called "hinting" instead of "checking"?

If I understand correctly what type hinting in php in fact does, it checks type and throws an error if it is not as declared.
That seems to me more than just a "hint" – more like a (full) "check" (like java - my main programming background).
So, I'm wondering if I'm missing something or what the intent was in naming it that way.
PHP does not check variable types and types are not declared within variable declaration. PHP always tries to convert variable to correct type when required. If you str_replace on number, number will be temporarily converted to string.
As you can see there http://php.net/manual/en/language.types.type-juggling.php
So its exactly the opposite from java.

What does <namespace>::class mean?

In a CakePHP Plugin documentation there is the following code line: $validator->provider('upload', \Josegonzalez\Upload\Validation\DefaultValidation::class);
\Josegonzalez\Upload\Validation\DefaultValidation is the namespace, but I didn't understand the ::class. Could someone explain it? I didn't find anything in PHP documentation.
the class constant simply returns the full name of the class (with namespace) as a string. So instead of passing as string to some method that requires it, you pass it the PHP way. It just looks nice, for example:
$validator->provider('upload',\Josegonzalez\Upload\Validation\DefaultValidation::class);
AND
$validator->provider('upload', '\Josegonzalez\Upload\Validation\DefaultValidation');
Both Are Same
And another advantage of this is that, if you need full class name several times in a single file.. say onto multiple method calls as a parameter. You can simply use it on the top & then only the classname will return the full name with namespace. like this:
use \Josegonzalez\Upload\Validation\DefaultValidation;
$validator->provider('upload', DefaultValidation::class);
//you can use it on other places as well, if required.
$someOtherClass->someOtherMethod(DefaultValidation::class);
So, in short, it reduces the number of characters you need to type, and makes your code look cleaner.

Detect all uppercase variables in PHP project (and replace with something else)

Firstly - some background. We have a config.php file which lists several variables and settings in this format:
$MY_EMAIL_ADDRESS = 'test#test.com';
$MY_WEBSITE = 'www.test.com';
$SOMETHING_ELSE = 'foobar';
I would like to replace them with more sensible (and secure) names as part of an array, throughout the entire PHP project. Mostly so we can do this more securely: Get PHP variable value via Ajax with variable name as parameter
We have also forgotten some of these variables names, so that they are used throughout the project, but possibly not documented - hence doing a search one-by-one will prove difficult.
Is there a way I can search the php files for any values that start with a dollar sign ($) and then are made up of only upper case letters and possibly underscores?
$MY_SETTING_NAME
We could then either build a list and update manually, or build some kind of script to replace things with a more sensible way of working:
$CONFIG['MY_SETTING_NAME']
Thank you!
You can use get_defined_vars function which will return all variables as array.
Please look at php.net website for example

i18n on a variable

I would like to know if I can use a gt function on a variable with php?
eg. : echo _($var);
Feel free to. But you need to make sure the possible contents of the variable makes it into .po/.mo files. (one of the ways to do assure it is to create a dummy file never processed except for by xgettext, containing _("translate me"); expressions).
I don't think gettext will recognize a variable since it scans the source code. If you want to include variables in a string, it's better to use
sprintf()
For example
echo sprintf(_("There are %d results!"), $numResults);
In gettext, the translator will see
There are %d results!
so therefore it can easily be translated as long as he/she knows that %d is a variable. When the script gets executed, gettext will first replace the translation, and then sprintf will insert the variable $numResults. Good luck! I just got done internationalizing my site.

Categories