Forums / Code Collaboration / Using PhpStorm

Using PhpStorm

Views: 21,659

Results 1 to 18 of 18
21 Jul 2018, 7:51 AM
#1
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Using PhpStorm

The inspections in PhpStorm flag up constants that it cannot find, such as those in the database, as errors.

As far as I can tell, the only way to stop these errors is to have the database constants (configuration_keys) duplicated in a file that PhpStorm CAN find/index.

So I did a script that generates a suitable file:
https://github.com/torvista/zen-cart_list-configuration-constants

Constructive criticism welcome!

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

24 Jul 2018, 8:56 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Using PhpStorm

Great idea.

As for naming convention, I have both my .gitignore and my FTP app set to ignore all "dev-*.php" files, so that they aren't committed to VCS nor uploaded to servers.
Alternatively, other utility tools often use underscore prefixes, for example one of them is: _ide_helper.php
(I avoid using dot-prefixed files for semi-sensitive things like this, just in case I accidentally ignore them because they're hidden-by-default.)

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

8 May 2019, 8:58 AM
#3
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

Is there any way to get these sql inspections to work, instead of switching them off?
Attachment 18434

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

8 May 2019, 10:40 AM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Using PhpStorm

torvista:

Is there any way to get these sql inspections to work, instead of switching them off?
Attachment 18434
I haven't tried it in a while, but supposedly if a connection to a database was made, then those type of notifications would go away. I found this related to making that connection: https://www.jetbrains.com/help/phpstorm/connecting-to-a-database.html

Last time I tried to do this a couple of years ago I ran into some sort of issue that I'm sure was my fault or doing.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

8 May 2019, 11:40 AM
#5
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

The problem is not the database connection (that is working ok), but that PhpStorm cannot read/substitute the database constants in those queries for the constant definitions in
includes/database_tables.php with the DB_PREFIX.

So, this does not work:
define('TABLE_PRODUCTS', DB_PREFIX . 'products');

but this does work (clears the error)
define('TABLE_PRODUCTS', 'products');

web searches indicate this is not possible, I was just wondering if anyone had found a way to make it work without changing includes/database_tables.php.

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

5 Dec 2019, 5:04 PM
#6
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

There is a PHP inspection plugin for Phpstorm called EA Extended: another world of php nitpicking to spend endless hours with, but the principal things it spits out are

recommending strict comparisons when possible.
ie.
if ($action == 'edit')...
to
if ($action === 'edit')...

and using single quotes around sql text chunks

As there are so many of them to initially correct, it's hard to see any real issues (can't see the wood for the trees).

For ongoing code/PR submissions, should I "correct" these things or ignore them?

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

5 Dec 2019, 10:01 PM
#7
lankeeyankee avatar

lankeeyankee

Totally Zenned

Join Date:
Jan 2007
Posts:
1,514
Plugin Contributions:
1

Re: Using PhpStorm

torvista:

The problem is not the database connection (that is working ok), but that PhpStorm cannot read/substitute the database constants in those queries for the constant definitions in
includes/database_tables.php with the DB_PREFIX.

So, this does not work:
define('TABLE_PRODUCTS', DB_PREFIX . 'products');

but this does work (clears the error)
define('TABLE_PRODUCTS', 'products');

web searches indicate this is not possible, I was just wondering if anyone had found a way to make it work without changing includes/database_tables.php.

Did you ever find a workaround? I have modified includes/database_tables on my local install to avoid the issue.


Zen Cart and it's community are the best!!

5 Dec 2019, 10:07 PM
#8
lankeeyankee avatar

lankeeyankee

Totally Zenned

Join Date:
Jan 2007
Posts:
1,514
Plugin Contributions:
1

Re: Using PhpStorm

torvista:

There is a PHP inspection plugin for Phpstorm called EA Extended: another world of php nitpicking to spend endless hours with, but the principal things it spits out are

recommending strict comparisons when possible.
ie.
if ($action == 'edit')...
to
if ($action === 'edit')...

and using single quotes around sql text chunks

As there are so many of them to initially correct, it's hard to see any real issues (can't see the wood for the trees).

For ongoing code/PR submissions, should I "correct" these things or ignore them?

I'll have to install that, thanks for pointing it out. I think it's important moving forward to future-proof so think making the changes as the code is being worked on would be the best way. That's what I've been doing, and the amount of warnings when the log all errors mod is on is steadily shrinking.


Zen Cart and it's community are the best!!

5 Dec 2019, 10:29 PM
#9
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

Did you ever find a workaround?
No. I did the same.

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

8 Dec 2019, 9:03 AM
#10
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

To prevent php inspection flagging up multiple errors as "undefined variables" for uses of messagestack and zco_notifier, you can add this to the start of the file you are working on, temporarily:

/**

  • @var messageStack $messageStack
  • @var zcObserverLogEventListener $zco_notifier
    */

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

26 Apr 2020, 10:28 AM
#11
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

inspection error "undefined constant" when constants are in arrays.
Currently ZC156, constants are in files and phpStorm can find them.

In the ZC157 Installer, the constants are in an array, main.php and phpStorm cannot find them/flags all of their uses as undefined.

Since I believe this method of defining constants is the plan for the future, a method to correct this inspection would be of use.
Apart from using a separate script to create a file with a static list as done for the db constants it cannot find either (https://github.com/torvista/zen-cart_list-configuration-constants),

anyone have a solution?

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

12 Jan 2021, 10:37 PM
#12
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

Regarding consistent coding standards, there is an .editorconfig file in the zencart GitHub repository.
I note the composer file specifies the php levels but I don't see anything else specific to the editor (like PSR-2).
Is there anything else that will indicate to IDEs how code should be laid out etc. or we should just set PSR-2 manually and there is nothing else to do?

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

12 Jan 2021, 11:53 PM
#13
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Using PhpStorm

Unfortunately .editorconfig doesn't enforce language-specific coding standards, just small things like spacing and line-endings.

To implement PSR-2 in PhpStorm, in your Editor->Code Style settings, go to PHP and click the "set from..." link, choose "PSR2."
Then, press CTRL+ALT+L to reformat your code ... either for a highlighted selection of code or for the entire current file.

You can set PhpStorm's settings to apply per-project or for all projects.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

13 Jan 2021, 2:06 AM
#14
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,973
Plugin Contributions:
8

Re: Using PhpStorm

DrByte:

Unfortunately .editorconfig doesn't enforce language-specific coding standards, just small things like spacing and line-endings.

To implement PSR-2 in PhpStorm, in your Editor->Code Style settings, go to PHP and click the "set from..." link, choose "PSR2."
Then, press CTRL+ALT+L to reformat your code ... either for a highlighted selection of code or for the entire current file.

You can set PhpStorm's settings to apply per-project or for all projects.

so there is no real way to export a settings file from phpstorm that has all of the ZC codings standard already encapsulated in it?

asking for a friend.... :smile:

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

13 Jan 2021, 3:30 AM
#15
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Using PhpStorm

carlwhat:

so there is no real way to export a settings file from phpstorm that has all of the ZC codings standard already encapsulated in it?

asking for a friend.... :smile:

As far as a settings file for code-formatting, PSR-2 (or PSR-12 is probably fine too, although a lot of ZC code doesn't use things it addresses) is about all you need for code such as the v158 branch. (Or, at least, in this moment that's all that comes to mind.)
Lots of other settings around using the IDE or its appearance are more personal preference.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

11 Jun 2021, 7:13 AM
#16
marcopm avatar

marcopm

New Zenner

Join Date:
Jun 2021
Posts:
42
Plugin Contributions:
1

Re: Using PhpStorm

torvista:

The problem is not the database connection (that is working ok), but that PhpStorm cannot read/substitute the database constants in those queries for the constant definitions in
includes/database_tables.php with the DB_PREFIX.

So, this does not work:
define('TABLE_PRODUCTS', DB_PREFIX . 'products');

but this does work (clears the error)
define('TABLE_PRODUCTS', 'products');

web searches indicate this is not possible, I was just wondering if anyone had found a way to make it work without changing includes/database_tables.php.

PhpStorm 2021.1 now supports prefixed table names: https://blog.jetbrains.com/phpstorm/2021/03/phpstorm-2021-1-eap-6/#support_for_prefixed_table_names_in_sql_wi
However I don't know exactly how to make it work.
I tried making the file .phpstorm.meta.php (the tables have no prefix):

namespace PHPSTORM_META {
  override(
  // Virtual function to indicate that all SQL
  // injections will have the following replacement rules.
    sql_injection_subst(),
    map([
      "DB_PREFIX" => "",
    ])
  );
}

but the errors remain.

However in the article it says:

This works with concatenation as well. See the following example:

<?php const DB_PREFIX = "mydatabase_"; $sql = "SELECT * FROM " . DB_PREFIX . 'table_name'; ``` which means, I guess, that the previous *.phpstorm.meta.php* is not needed and you need to use *const* instead?? :huh:
14 Jun 2021, 9:02 AM
#17
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

Yes I saw that, but I find that phpStorm now finds the table definition with the prefix, without having done anything: there is no error, with SQL all inspections enabled...so I don't know how that happened, but it's no longer flagging an error for me.

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

20 Aug 2026, 10:10 AM
#18
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,876
Plugin Contributions:
7

Re: Using PhpStorm

GitHub Repo/V3 - phpstan inspection

I note that if the not_for_release folder is not present, the PhpStorm-phpstan inspection complains that the it cannot find any of the functions in the file in question.
(Since I am using this fileset in production, I have been omitting this folder in the local/dev server just to be sure there is nothing in there that the main fileset may be referencing.)

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...