PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 19, 2018 8:08 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Feb 19, 2013 3:47 am 
Offline
Forum Newbie

Joined: Tue Feb 19, 2013 3:42 am
Posts: 2
I've been assigned a project that have associative arrays without single quotes all over the place.

Example:

$foo[bar]

Which should be:

$foo['bar']

This generates a PHP notice and it's very bad practice.

With this command in Vim I can replace all "foo" instances with "bar" in the current directory and subdirectories

:args **/*.php | argdo %s/foo/bar/ge | write

I would like to apply the same principle with a regular expression in order to fix the associative arrays.

So instead of 'foo' there should be a regular expression that finds associative array keys and instead of "bar" another regular expression that inserts single quotes.

Any suggestions?

May be this request for help is too difficult or not even possible, in that case I would take any recommendation on how to approach this problem.


Top
 Profile  
 
PostPosted: Tue Feb 19, 2013 4:14 am 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
I'd just use a simple substitution like '[' -> "['", plus manual confirmation of each replace (:help s)


Top
 Profile  
 
PostPosted: Tue Feb 19, 2013 8:35 am 
Offline
Forum Newbie

Joined: Tue Feb 19, 2013 3:42 am
Posts: 2
The problem with that approach is that there are many files with this problem, it could take me a very long time to do this manually.

Furthermore in that way it would also find instances of indexed arrays.


Top
 Profile  
 
PostPosted: Tue Feb 19, 2013 3:33 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
the problem with regex is that it's not really suited for such a task, thus you'd have either accept some false positives
(like arrays with integer indices) or some false negatives. Proper solution (in academic sense) would require you to properly lex php code (which is quite simple thanks to token_get_all() function) and apply some parsing (to support cases like $arr[ind . $otherArr[otherInd]]).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group