Code Monkey home page Code Monkey logo

Comments (1)

jrfnl avatar jrfnl commented on July 2, 2024

@kkmuffme Thanks for opening this issue. That's a nasty one and you do have a point.

Since this is an extremely rare occurrence, most people don't know that this can possibly happen or aren't aware.

I'm not sure the overhead of always requiring addcslashes() for all preg_replace() calls containing a variable in the $replacement parameter is the right solution for something which, by your own admission, will be pretty rare in practice.

I mean, when such a sniff would be used, the only way to "solve" the issue (aside from ignoring it) is to add the function call, even when it is not needed as you know the variable used in safe, which it will probably be in > 95% of all cases.

All in all, I'm not adverse to such a sniff, but I consider it low priority and up for grabs if someone wants to work on it.


Some notes for if/when someone would want to work on this:

  • The sniff should only flag $replacement parameters containing a T_VARIABLE token as one of the tokens in the parameter.
  • The sniff would need to bow out if parameter unpacking is used preg_replace( $search, ...$otherParams).
  • The sniff would need to take into account that the $replacement parameter can either be a string or an array.
    • If the parameter passed is a variable, whether it is a string or an array cannot be determined (based on the function call alone), so this can be flagged, but most definitely not auto-fixed.
    • If the parameter passed is clearly an array (because the array is defined within the parameter), each array entry will need to be examined and, if necessary, flagged individually.
    • If the parameter passed/array item is clearly a string, it should be possible to auto-fix this, with a preference for fixing this by wrapping the whole array item/parameter in a function call to addcslashes() to prevent potentially having to add the function call multiple times if multiple variables are being concatenated together, though the implementation details of addcslashes() should be checked to be sure this is the right fix.
  • The sniff would need to take PHP 8.0 named parameters into account. This will also impact any potential auto-fixers.
  • The sniff would need to take a replacement array with keys into account. This will also impact any potential auto-fixers.

So, yes, this sniff will be pretty complex to write.

from phpcsextra.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.