overview

varScoper is a cfc and corresponding views that are designed to identify variables created within a cffunction that don't have a corresponding (cfset var) statement.

If you have never missed a single (cfset var) statement when writing code, you can stop reading now. If you don't know what (cfset var) is for, then you absolutely need to take a look at this code..

resources
If you are not quite sure why scoping variables within cffunctions is so important, you should take a look at the following resources.

Raymond Camden's guide to variable scopes (pdf)

Properly scoping variables is extremely critical especially as you start to cache cfc instances in shared scopes(session, application, server). mach-ii and modelglue users should definitely be aware of this.

examples
The following is a link to a live example of varScoper. It parses and identifies unscoped variables within a few of the popular open source ColdFusion frameworks. You might be surprised with the results.

Link to varScoper example

features
Here are some of the things that varScoper will do

  • Find unscoped variables created with a cfset within a cffunction
  • Find unscoped variables created with cftags (cfloop, cfquery, etc)
  • Find unscoped variables and var statements within cfscript (excluding comments)
  • Report line numbers of statements along with functions
  • Link to a displayable copy of the file with line numbers
  • Parse a single file, directory, or directory tree using absolute or relative paths
  • Output to screen, or csv
  • Fast and Lightweight: In my local environment (P4M 2.0, 2gbRAM, winXP), I processed 1126 files and 9030 cffunctions in 42594ms
varScoper currently supports the following tag:variable combos, please send me any additions.
  • cfloop:index
  • cfloop:item
  • cfquery:name
  • cfinvoke:returnvariable
  • cfdirectory:name
  • cffile:variable
  • cfparam:name
  • cfsavecontent:variable
  • cfform:name
  • cfhttp:result
  • cfquery:result
  • cfimage:name
  • cfmail:query
  • cffeed:name
  • cffeed:query
  • cfftp:name
  • cfwddx:output
  • cfobject:name
  • cfsearch:name
  • cfprocresult:name
  • cfpop:name
  • cfregistry:name
  • cfreport:name
  • cfdbinfo:name
  • cfdocument:name
  • cfexecute:variable
  • cfNtAuthenticate:result
  • cfcollection:name
  • cfpdf:name
  • cfxml:variable
  • cfzip:name
  • cfldap:name

known limitations
Here are some of the things that varScoper won't do
  • Promise to find all unscoped variables: varScoper is a work in progress. I've accounted for as many test cases as I can think of, but not all. Please help by extending the testCaseCFC
  • Display Line numbers for cfscript variables (It will tell you the function, but not the line number
  • Ignore comments: (Fixed in 1.20) From what I can tell, I think I would need lookbehind within RegEx to do this. CF RegEx doesn't support this, so I would need to use another implementation. Any help on this would be appreciated. (update: cfscript parsing is able to ignore comments )

DISCLAIMER: varScoper is only intended to identify POSSIBLE scoping problems within your code. All code needs to be thoroughly analyzed to verify that a variable should be locally scoped within a function.

revision history
  • 7/22/09 - 1.3
    • CF Builder Extension Support
    • Recognizes var statements anywhere in a function (CF9)
    • Issues (13,14,15,16,17,18,19,20,21,23,25,26,27,28,30,31,32) Fixed
    • Open BD/Railo Supported
    • CFScript comments parsed correctly
  • 5/22/08 - 1.21 - CF7,CF6, *nix bugs, issue #4 updated.
  • 5/20/08 - 1.20
    • Significant improvements to cfscript parsing engine
    • Issues (6,7,8,9,10,11) fixed
    • Ability to exclude files/folders using properties.xml (only when parsing a folder)
    • Ability to identify tags with "multiple personalities" i.e. cffeed/cfprocparam that can have different behaviors for output variables based on params
    • More agressive var scope checking (newly identified scenarios that were missed before)
    • Addition of unit testing suite
  • 12/6/07 - 1.12 - Addition of new tags to parse, XML output added, proper directory parsing for CF 6/7/8.
  • 10/7/07 - 1.1 - Updates to support parsing of cfscript, file paths work across all platforms
  • 7/20/06 - 1.0 - initial release

BlogCFC was created by Raymond Camden. This blog is running version 5.9.002.