4

I have a lot of FW/1 controllers which can perform differing functions based on whether the request is a get or post. I have a lot of code that looks like this

if (cgi.request_method == "post")   {

I recently installed Commandbox's CodeChecker CLI and I turned it loose on my controllers. I am getting warnings like:

Don't use shared scope variables in a CFC | Standards | Severity: 4
  Avoid using shared scope variables from within a CFC as it breaks encapsulation.
  Users//jamesmohler/Sites/ColdFusion/P.../messagesController.cfc:13

I have gone back to the FW/1 Reference Manual , and I have noted that it has a function called getCGIRequestMethod()

Question

Have I been testing for POST wrongly all along? What exactly am I being encouraged to avoid?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • 2
    In the strictest terms I guess that would break encapsulation because you are accessing a "global" variable directly from your method. I think it is similar to the answer here - https://stackoverflow.com/a/35818502/1636917 – Miguel-F Oct 16 '18 at 12:18
  • Along those same lines, I think the recommendation of avoiding shared scopes is to reduce dependencies with the theory that objects are more flexible if the needed values are passed in as arguments, rather than accessed directly through a shared scope. Though not sure what the alternative would be in this case, other than using getHTTPRequestData().method. – SOS Oct 16 '18 at 15:01
  • 3
    I would also say that if `cgi.request_method` (or another globally accessible scope method) changed to `cgi.requestMethod`, then you'd have to change every place `request_method` was referred to, rather than just changing it in `getCGIRequestMethod()`. I can see both sides. The whole point of the `CGI` scope was that it is available to the entire request, pretty much no matter what language you are using. But looking at it with a wider lens, it's still a global variable, and global variables (by definition) can be modified outside of the piece of code you are working with. – Shawn Oct 16 '18 at 15:40
  • At some point there has to be logic that decides what the payload is and how to treat it. On that note (I don't know FW/1), are there no http verb specific controllers supported? Basically what `doGet` and `doPost` offers in Java servlets. Those would be invoked based on the CGI data and have the payload passed. – Alex Oct 16 '18 at 19:58

1 Answers1

0

Short answer

I have replaced

 if (cgi.request_method == "post")   {

with

 if (framework.getCGIRequestMethod() == "post")   {

Long answer

FW/1 does tap into other CGI variables, but does not expose them. So there are no similar functions I can tap into.

FW/1 copies the data into

request._fw1 = {
   cgiScriptName = CGI.SCRIPT_NAME,
   cgiPathInfo = CGI.PATH_INFO,
   cgiRequestMethod = CGI.REQUEST_METHOD,
   ...

Which begs the question of request. scope is better than cgi. scope. I hereby submit it does not because both happen at the time of the processing request. Using cgi. might break encasulation, but I don't think pushing in variables via FW/1's rc. scope desirable. I also don't want to modify FW/1 to capture all cgi variables.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72