Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Php8.2 Contact import Map Field screen - remove undefined property usage (mutliple) #25298

Merged
merged 12 commits into from
Jan 18, 2023

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented Jan 9, 2023

Overview

Removes rather than declares _columnCount (https://github.com/civicrm/civicrm-core/pull/25296/files#diff-4f0a1c0d1f7a02c448ebe273bd5b951a1811dd14949b11b9c8914b2c198fceba was going with declare

This is a legacy property

Before

We can see the assign is never used (only in search related forms)
image

We can see that _columnCount is only still used in the Contact MapField form - the others simply loop through the headers (rather than counting them & the using that number in a for loop)

foreach ($columnHeaders as $i => $columnHeader) {

Unused variable
image

_dataValues is only used for an unused function

image

After

Note there are still 7 rows - that is what the columnCount was used to display

image

Technical Details

Comments

@civibot
Copy link

civibot bot commented Jan 9, 2023

(Standard links)

@civibot civibot bot added the master label Jan 9, 2023
@eileenmcnaughton eileenmcnaughton changed the title Php8.2 Contact import - remove columnCount Php8.2 Contact import - remove undefined property usage _columnCount, _contactSubType Jan 9, 2023
@eileenmcnaughton eileenmcnaughton changed the title Php8.2 Contact import - remove undefined property usage _columnCount, _contactSubType Php8.2 Contact import Map Field screen - remove undefined property usage _columnCount, _contactSubType Jan 9, 2023
@eileenmcnaughton eileenmcnaughton changed the title Php8.2 Contact import Map Field screen - remove undefined property usage _columnCount, _contactSubType Php8.2 Contact import Map Field screen - remove undefined property usage (mutliple) Jan 9, 2023
@eileenmcnaughton
Copy link
Contributor Author

I did an r-run on this & also decided to fix some of the notices I encoutered at the smarty level

#25302

@@ -174,12 +171,12 @@ public function buildQuickForm() {
$id = $first = $second = NULL;
}
if (($first === 'a' && $second === 'b') || ($first === 'b' && $second === 'a')) {
$cType = $contactRelationCache[$id]["contact_type_{$second}"];
$cType = $contactRelationCache[$id]["contact_type_$second"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subjective aside: I find this old expression more readable than this new expression. (With the new one, my eyes have to re-scan a few times to confirm that $ really is there and recognize that second is a variable-name.)

Granted, Github UI and PHPStorm UI render this differently. PHPStorm's highlighter changes the color of $second (which serves a similar purpose of helping you recognize the variable-name). I suppose that (within PHPStorm) the {}s are visually redundant. Maybe that's why they nag you to remove them.

But other highlighters don't work the same way. The change makes the code less readable in other environments.

PHPStorm's nag feels counter-productive to me. It can be fixed, though:

Screen Shot 2023-01-10 at 5 13 14 PM

Of course, both forms are legal, and it doesn't affect the mergability of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@totten - yes I generally figure that when it comes to preferences I'd rather go with what has been generally agreed elsewhere rather than come up with our our negotiated version of what is more readable. (My own readability preference in this case would be to concatenate it onto the end :-)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • as an aside - I've seen several things that phpstorm encouraged in the main inspections & the Extended ones become required as we've gone up the php versions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • on the readability I look at that line & think - WTF is 'cType' so I don't get to the quoted part before my brain refuses to parse it so that's an 'unscannable' line from my POV

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I wound up looking at this I decided to fix the variable names & then I wound up extracting the determination of the value - as is usually the case in civi those curly braces in a string point to a bit of code that is just nasty

Copy link
Member

@totten totten Jan 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on the readability I look at that line & think - WTF is 'cType' so I don't get to the quoted part before my brain refuses to parse it so that's an 'unscannable' line from my POV

Heh. Yeah. cType probably felt natural to whoever wrote it (at the time). Of course, if I look at that out-of-context, then I think: "oh, C language type -- void* or char**! Wait, why are we using C types in PHP?" 🙃

... I wound up extracting the determination of the value ...

If you're looking at some code and find some change which (subjectively) seems easier to read/understand, then power to you. 🚀

And yes - I was right - phpstorm IS signalling an upcoming deprecation https://php.watch/versions/8.2/$%7Bvar%7D-string-interpolation-deprecated

Just noting that this was incorrect, and it seems sort of material to topic. Both php.watch and the original RFC say that {$var} is preferred over ${var} (since the latter is duplicative and less general).

But the language benefits from having some flavor of {} , because bare $var can be ambiguous. I haven't seen any authority say {$var} is going away.

I'd rather go with what has been generally agreed elsewhere rather than come up with our our negotiated version of what is more readable

Let's take that and treat it as a question. What is the generally agreed approach from elsewhere vis-a-vis standards for variables-in-strings? Is it:

  1. "foo_$bar"
  2. "foo_{$bar}"
  3. 'foo_' . $bar
  4. Other/None of the above

For "elsewhere", we can look to large communities like drupal.org, wordpress.org, php-fig.org, and symfony.com. They all use PHP variables. They all have coding standards. But none of them offers an answer to this question. (There do address some narrower cases -- like t() for Drupal prose and sprintf() for Symfony error-messages -- but this isn't prose. There's nothing that applies to this kind of string.)

Why not? Are Drupal developers too shy to haggle over obscure details? Is this a newfangled problem that coding-scientists have never seen before? No... I submit that the absence points to answer (4). They don't standardize this because it's not worth doing. (The cost of standardizing exceeds the benefit.)

It seems to me that PHPStorm is the outlier, and... even so, PHPStorm is rather non-committal (since it puts the "Disable" option right there in the popup...).

(To be clear -- I'm not saying that we should ignore everything PHPStorm says. I'm saying we should ignore it when it's wrong. 😈)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@totten I think the 'disable option right there in the popup' is a bit of a red herring - that option is there for all inspections.

But since you seem to care about this one I'll disable it. I go with phpstorm because it is a standard, picks up lots of good stuff - some of which have since been enforced, and helps us avoid bike shedding the details. But in terms of readability I think using actual words is much more important in this code than how the string is concatenated

@eileenmcnaughton
Copy link
Contributor Author

These are real test fails - I'll take a look

Test Result (9 failures / +8)CRM_Contact_Import_Form_MapFieldTest.testLoadSavedMappingDirectCRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #0CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #1CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #2CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #3CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #4CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #5CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #6

Test Result (9 failures / +8)
CRM_Contact_Import_Form_MapFieldTest.testLoadSavedMappingDirect
CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #0
CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #1
CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #2
CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #3
CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #4
CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #5
CRM_Contact_Import_Form_MapFieldTest.testDefaultFromColumnNames with data set #6

@eileenmcnaughton
Copy link
Contributor Author

I just checked the testDefault tests and put up a fix - it's a bit arguable

The test checks we guess at the default from the column header. The test uses an sql datasource not a csv - I'm not terribly sure that is valid - but .... I can use the equivalent of the former code to make it continue to work per the test

@eileenmcnaughton
Copy link
Contributor Author

test this please

@seamuslee001
Copy link
Contributor

Jenkins re test this please

@eileenmcnaughton
Copy link
Contributor Author

test this please

1 similar comment
@eileenmcnaughton
Copy link
Contributor Author

test this please

@colemanw
Copy link
Member

Code cleanup makes sense and this is well tested.

@colemanw colemanw merged commit a1c3dbf into civicrm:master Jan 18, 2023
@colemanw colemanw deleted the import branch January 18, 2023 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants