Skip to content

Commit

Permalink
kill-split: split() -> preg_split()
Browse files Browse the repository at this point in the history
Test Plan:
~ cat a.php
<?php
if (4 >= $a) {
  echo $lol;
}
if ($a === $b) {
  echo $lol;
}
split('l\\\\o/l', 'aaal\\o/lbbb');

~ lex-pass kill-split
Checking (1/1) a.php
- Parsing
- Saving

~ cat a.php
<?php
if (4 >= $a) {
  echo $lol;
}
if ($a === $b) {
  echo $lol;
}
preg_split('/l\\\\o\/l/', 'aaal\\o/lbbb');

~ phpsh
php> =split('l\\\\o/l', 'aaal\\o/lbbb')
PHP Deprecated:  Function split() is deprecated in /usr/lib/python2.6/site-packages/phpsh/phpsh.php(534) : eval()'d code on line 1
array(
  0 => "aaa",
  1 => "bbb",
)
php> =preg_split('/l\\\\o\/l/', 'aaal\\o/lbbb');
array(
  0 => "aaa",
  1 => "bbb",
)
  • Loading branch information
dancor committed Jul 3, 2010
1 parent 5e4c488 commit b160693
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Lang/Php/Ast.hs
Expand Up @@ -3,6 +3,7 @@
module Lang.Php.Ast (
module Lang.Php.Ast.Common,
module Lang.Php.Ast.Expr,
module Lang.Php.Ast.Lex,
module Lang.Php.Ast.Stmt,
Ast
) where
Expand Down
40 changes: 34 additions & 6 deletions src/Transf/AntiPattern.hs
Expand Up @@ -14,11 +14,14 @@ transfs :: [Transf]
transfs = [
"assignables-go-right" -:- ftype -?-
"\"if ($x == true)\" -> \"if (true == $x)\" etc"
-=- (\ [] -> lexPass $ assignablesGoRight)]
-=- (\ [] -> lexPass $ assignablesGoRight),
"kill-split" -:- ftype -?-
"split() becomes preg_split()"
-- TODO: detect non-regex case and go to explode() instead of preg_split()?
-=- (\ [] -> lexPass $ killSplit)]

exprIsLRVal :: Expr -> Bool
exprIsLRVal (ExprRVal (RValLRVal _)) = True
exprIsLRVal _ = False
assignablesGoRight :: Ast -> Transformed Ast
assignablesGoRight = modAll . modIfBlockExpr $ modWSCap2 exprLRValToRight

exprLRValToRight :: Expr -> Transformed Expr
exprLRValToRight (ExprBinOp op e1 w e2)
Expand All @@ -34,6 +37,31 @@ exprLRValToRight (ExprBinOp op e1 w e2)
else transfNothing
exprLRValToRight _ = transfNothing

assignablesGoRight :: Ast -> Transformed Ast
assignablesGoRight = modAll . modIfBlockExpr $ modWSCap2 exprLRValToRight
exprIsLRVal :: Expr -> Bool
exprIsLRVal (ExprRVal (RValLRVal _)) = True
exprIsLRVal _ = False

killSplit :: Ast -> Transformed Ast
killSplit = modAll $ \ a -> case a of
ROnlyValFunc _c@(Right (Const [] "split")) w (Right (arg0:args)) ->
case arg0 of
WSCap w1 (Left (ExprStrLit (StrLit s))) w2 ->
pure . ROnlyValFunc c' w . Right $ arg0':args
where
c' = Right (Const [] "preg_split")
arg0' = WSCap w1 (Left (ExprStrLit (StrLit s'))) w2
s' = onTail (onInit $ delimify '/' '\\') s
_ -> transfNothing
_ -> transfNothing

delimify :: (Eq a) => a -> a -> [a] -> [a]
delimify delim esc s = [delim] ++ concatMap doEsc s ++ [delim] where
doEsc c = if c == delim then [esc, c] else [c]

onTail :: ([a] -> [a]) -> [a] -> [a]
onTail f (x:l) = x : f l
onTail _f l = l

onInit :: ([a] -> [a]) -> [a] -> [a]
onInit = reversify . onTail . reversify

0 comments on commit b160693

Please sign in to comment.