Code (style) normalisation
dvinegla - May 26, 2008 - 09:09
| Project: | Pathauto |
| Version: | 5.x-2.x-dev |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Description
There are places with:
<?php
$pattern = variable_get('bar', FALSE);
if (!trim($pattern)) {
?>and others with:
<?php
$pattern = trim(variable_get('bar', ''));
if (!$pattern) {
?>... and some white spaces.
| Attachment | Size |
|---|---|
| pathauto_codestyle.patch | 5.18 KB |

#1
#2
You have a
$patern = '';which should be$pattern = '';. I'll leave this for Greg to review properly though, as he may know of a reason why the trims are done the way they are. (Also, you can find more (trailing) whitespace if you use Coder. ;))#3
$patern = '' -> $pattern = ''
#4
Patch doesn't apply cleanly anymore. Please re-roll.
#5
chasing HEAD.
#6
#7
The if (!$pattern) checks are not E_NOTICE compliant, which is something that a "code style" patch should also fix.
I'm also not sure that moving the trim() above is any clearer nor am I really sure that it's necessary...so, perhaps we can just drop that and change these to
if (isset($pattern) && $pattern) {The whitespace changes seem fine to me.
#8
This is why I always inicialize the variable $pattern: $pattern = '';
$pattern=trim(variable_get(.....
if ($pattern) {
you obtains a trimed pattern
$pattern = variable_get(...
if (trim($pattern)) {
you obtains a not trimed pattern
what do you prefer?
if you have a pattern like this: $pattern= ' ' (white space), it pass the check.
#9
This won't do:watchdog('pathauto', $update_msg);.watchdog()should be fed the "raw", untranslated string and then handle the translation when displaying the message in the logs. I'm not sure how to handle plurals withinwatchdog()though, so this might have to be looked into.Also, I'd vote for waiting with committing this for 6.x-2.x.D'oh! Commented on wrong issue. My bad. :x
#10
#11
Okay, here's the deal Greg and I just agreed on:
We want the variables stored with
trim(), ie.$pattern = trim(...).We want to use
!empty()to check that the variable exists (and contains something), ie.!empty($pattern). This should also remove the need to initialise the variables before time, right?Please let me/us know if we're out of our minds! :)
#12
re-roll and apply #11
#13
This is looking good. Thank you for your work on this! :)
If you're up to the task, there are some instances of
if (isset($var) && $var), and changing these toif (!empty($var))would be a nice addition to this code style clean-up. :) (If not, that's alright too, and this patch is close, if not ready, to being committed.)#14
I found if with "and" and others with "&&", change and to && or leave them?
#15
Hm. I'm not completely sure what you mean... ? Both
if (isset($var) and $var)andif (isset($var) && $var)should beif (!empty($var)), as far as I can tell... but yeah, if there are anyand's floating about outside of such a statement, they should probably be made to&&'s, for consistency. :)#16
if ($verbose and user_access('notify of path changes')) {
#17
Right, that should be changed to
&&(for consistency). :)Oh, and good catch!
#18
Just a note:
pathauto_user()has$new_user->roles = isset($edit['roles']) ? $edit['roles']: array();which should have a space before the colon. (This is only in the 6.x branch.)#19
A couple of hunks didn't apply anymore, so I re-rolled and incorporated some of the changes mentioned above.
#20
Looks good to me.
#21
While reading through the code again, I stumbled across this:
if (!isset($node->pathauto_perform_alias) || $node->pathauto_perform_alias)"If
$varis not set OR if it is true." Is this necessary? There are only ever so few cases where$node->pathauto_perform_aliaswon't fulfill either of those states... so I'm thinking that either one of them could probably go, or they both could... ?Similarly, instances like
if (isset($old_alias) && drupal_strlen($old_alias))could probably be replaced withif (!empty($old_alias))as well, as the only timedrupal_strlen($old_alias)will return something other than 0, is on a non-empty string. Thus, this should theoretically be an ever so slight performance increase as well. Any objections/reasons not to do this? :)An updated patch (without changing anything mentioned above) attached, which also includes some double to single string conversion.
#22
The pathauto_perform_alias stuff is very twitchy - it took a long time to work out a bunch of edge cases (user creates/edits nodes and does/doesn't have permission to create path aliases and node has a pattern set/doesn't but follows default). So, I'm hesitant to change that...
The second item you point out makes sense to me.
#23
Since
empty($var)returns true if$varis '0' (ie, a one character string containing the character "0"), we might not want to do this. But then, why not? If people "hardcode" their patterns to be "0", I'd say they're asking for trouble anyway. The only problem would be if people use something like "[title-raw]" (ie., where the title (or another, single token) is all there is in the pattern), where the title happened to be "0" - which might just happen. So I'm unsure of what to do. (I'll try and do some testing to see the actual results with and without this patch.)But here's an updated patch; including a re-roll, some array code style fixin', and the
empty()mentioned in the second part of comment #21. Also, I might commit a temporary patch based on this, without theempty()changes. I'll make a comment here if I do.#24
Eh. And the patch.
#25
Huh? Hm... third time's the charm, no?
#26
Hm. It seems I'm being hit by #267884: Attachments failing when users preview or use "attach" button... so I'll try again.
Edit: Yay! :D
#27
Okay, here's the deal on using
empty(), from what my quick testing revealed._not_zero(or something) to do both?#28
About bullet #2 above, check #268909: Path alias "0" not used for links for development on this. :)
#29
Anyway, the test returns no errors with current patch, so I committed this. If the
empty()'ies turn out to be problematic, we can weed through which ones are causing problems and try something else (if (empty($var) && strlen($var) < 1)perhaps?).Also, we should do this normalisation on the 5.x branch as well.
#30
I love how patches apply almost 1:1 between 6.x-1.x and 5.x-2.x. :)
If nobody is quick to step up and object to this, I'll apply it soonishly.
#31
And... committed.
#32
Automatically closed -- issue fixed for two weeks with no activity.