Sub topic 15
Data-flow analysis with the graph engine
This chapter assumes you can already run sf code-analyzer run with a workspace and a rule selector, and that you read results by severity and engine. It also assumes Java is installed, because the graph engine and PMD both run on Java.
How the graph engine differs
Most engines inspect code one file at a time. PMD, for example, looks at the syntax of a single class and flags a pattern such as a DML statement inside a loop. The graph engine, named sfge in the selector, works differently. It builds a graph of your Apex code across files and follows how data and control move through it. A finding can therefore depend on a path that crosses several classes and methods.
This lets it find problems that need context:
- A DML or query operation reached from a method that only appears safe in isolation.
- Possible null pointer exceptions, where a value can be null along some path.
- Missing field-level security or CRUD checks before a database operation, covered by the
ApexFlsViolationrule.
An example that needs a path
Neither class below looks wrong when read alone. Together, they run a DML statement without checking object permissions.
public with sharing class AccountController {
@AuraEnabled
public static void rename(Id accountId, String newName) {
AccountService.applyName(accountId, newName);
}
}
public with sharing class AccountService {
public static void applyName(Id accountId, String newName) {
Account acc = new Account(Id = accountId, Name = newName);
update acc;
}
}Each class goes in its own .cls file. The graph engine starts at the entry point, rename, follows the call into applyName, and reports that the update is reachable with no permission check on the way.
Running it
Select the engine by name:
sf code-analyzer run --workspace force-app --rule-selector sfge --view tableUse sf code-analyzer rules --rule-selector sfge first to see which rules are available in your installed version.
Workspace and target matter more here
Because the engine follows calls between files, the workspace must contain every class on the path. If you point --workspace at one folder and the called class lives elsewhere, the engine cannot follow the path and may miss findings. Keep the workspace broad, such as the whole force-app folder. Use --target to narrow which files are reported on and used as starting points.
sf code-analyzer run --workspace force-app --target force-app/main/default/classes/AccountController.cls --rule-selector sfgeTrade-offs
- Speed: path analysis takes much longer than a PMD scan, and it grows with project size.
- Where to run it: run it on pull requests or nightly builds. A fast engine is better for every local save.
- Noise: path-based rules can report paths your runtime never takes. Triage them as you would other findings, and suppress only with a written reason.
Exercise
You run the graph engine with --workspace force-app/main/default/classes/controllers, but your controller calls a service class stored in force-app/main/default/classes/services. Why might a real finding be missed, and how do you fix it?
Show answer
The engine can only follow calls into code that is inside the workspace. The service class is outside it, so the path ends at the call. Set --workspace to force-app (or a folder containing both), and use --target if you want to limit the report to the controller file.