Legacy PHP Refactoring 36: Adopting SmartyLint, Gradual Rule Un‐ignoring, and Updating Golden Masters

This post covers introducing static analysis (linting) for Smarty templates in a client project.

I installed smarty/smarty-lint (repository: umakantp/SmartyLint) as a linter for Smarty templates. It has a solid download count and ongoing maintenance and updates, so I decided to give it a try.

composer require --dev smarty/smarty-lint


Setup and Execution Environment

Because the existing templates contain many errors, enforcing every rule across all files from day one would quickly become overwhelming. Instead, I started by running the linter in CI only on changed .tpl files, while making it callable locally through a Makefile.

GitHub Actions Workflow (Initial: Changed Files Only)

Using dorny/paths-filter@v4, the workflow detects additions or modifications under templates/**/*.tpl and runs smarty-lint only when matching files exist.

    # ========================================
# File Change Detection
# ========================================
- name: Detect changed files
uses: dorny/paths-filter@v4
id: paths_filter
with:
# Get a space-separated list of added or modified files
list-files: shell
filters: |
tpl:
- added|modified: 'templates/**/*.tpl'

# ========================================
# Smarty Template Linting
# ========================================
- name: Run smarty-lint
if: ${{ steps.paths_filter.outputs.tpl == 'true' }}
shell: bash
run: composer run-script smarty-lint ${{ steps.paths_filter.outputs.tpl_files }}

composer.json and Makefile

I added a script entry in composer.json and wrapped it in a Makefile target so it runs inside the PHP container.

{
  "scripts": {
    "smarty-lint": "php vendor/smarty/smarty-lint/smartyl --rules=smarty-lint.rules.xml"
  }
}

.PHONY: smarty-lint
smarty-lint:
	docker compose run --rm php-web composer run-script smarty-lint templates


Initial Rule Configuration (smarty-lint.rules.xml)

Next, I created smarty-lint.rules.xml and placed rules that did not fit our workflow or conflicted with how comments are written in the codebase under <ignore>.

<?xml version="1.0" ?>
<rules>
  <conf>
    <leftDelimiter>{</leftDelimiter>
    <rightDelimiter>}</rightDelimiter>
    <autoLiteral>true</autoLiteral>
    <extensions>
      <extension>tpl</extension>
    </extensions>
  </conf>
  <ignore>
    <rule name="Commenting.FileComment" />
    <rule name="Commenting.FileNoComment" />
    <rule name="Commenting.Fixme.TaskFound" />
    <rule name="Commenting.Todo.TaskFound" />
  </ignore>
</rules>

Why These Rules Were Ignored

  • **Commenting.FileComment / Commenting.FileNoComment** These require file-level docblocks and @param tags like the author's sample (demo/good_file.tpl). While nice to have for documentation, the linter does not verify whether @param types or variable names actually match the template, so I disabled these rules and left comments to each developer's discretion. https://github.com/umakantp/SmartyLint/blob/master/demo/good_file.tpl
{**
 * Display's user information.
 *
 * @param string $name    Name of user.
 * @param string $school  School name of user.
 * @param string $school2 School name of user.
 *}
Name : {$name} <br>
Company : SmartyLint <br>
School : {$school} <br>

  • **Commenting.Fixme.TaskFound / Commenting.Todo.TaskFound** These flag TODO and FIXME inside comments. Ignoring them avoids making developers hesitant to jot down quick investigation notes or refactoring reminders.

Gradual Rule Enablement and AI-Assisted Fixes

Rather than turning on every rule at once and doing a massive cleanup, I followed a step-by-step approach: temporarily ignore rules, remove one rule at a time, have the AI fix the resulting errors, and open a PR.

First, I asked the AI to generate a list of current errors.

Run `make smarty-lint` and create a list of errors.


Code language: JavaScript (javascript)

Then, for local verification (not committed), I added the following rules to <ignore> in smarty-lint.rules.xml so I could un-ignore them one by one:

    <rule name="Files.EndFileNewline.NoneFound" />
    <rule name="Files.EndFileNewline.TooMany" />
    <rule name="Indent.DisallowTabIndent.TabsUsed" />
    <rule name="Whitespace.SuperfluousWhitespace.EmptyLines" />
    <rule name="Whitespace.TagsWhitespace.Smarty" />

Why Filtering to One Rule in Config Is More Consistent Than Prompting for It

Instead of dumping a huge error log and telling the AI "pick out only this rule and fix it," narrowing the linter configuration to a single rule and giving a simple instruction like "fix the reported errors" keeps the AI's behavior much more consistent.

  • Less noise and better focus on target lines: When many unrelated rule violations appear in the output, it is easier to miss items or make unintended edits. Limiting the output to one rule lets the AI focus on the exact file paths and line numbers.
  • Simpler instructions and fewer judgment calls: Removing "filter by specific rule" conditions from the prompt turns the task into a single goal: resolve whatever make smarty-lint reports.
  • Easier diff reviews: Each PR or diff contains changes from a single rule, making it straightforward to verify that no unrelated edits slipped in.

1. Resolving Whitespace.TagsWhitespace.Smarty (No HTML Output Impact)

Remove Whitespace.TagsWhitespace.Smarty from smarty-lint.rules.xml. Running make smarty-lint should now report only Whitespace.TagsWhitespace.Smarty errors.

For example, extra whitespace right before the closing } of a single-line Smarty tag is reported like this:

----------------------------------------------------------------------
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
----------------------------------------------------------------------
 6 | ERROR | Whitespace found before end of one line smarty tag
   |       | (Whitespace.TagsWhitespace.Smarty)


Before (unnecessary space before closing brace):

{if $value }

After (expected code):

{if $value}

Here is the step-by-step flow:

  1. Have the AI run make smarty-lint and inspect the file paths, line numbers, and error messages.
  2. Instruct the AI to fix them.
  3. Because adjusting whitespace inside Smarty tags should not change rendered HTML output, run the Golden Master tests to verify:
Run `make php-test-golden-master`.


Code language: JavaScript (javascript)

Once verified, check the diff, commit, open a PR, and review.

2. Resolving Files.EndFileNewline.NoneFound (Golden Master Update Required)

Remove Files.EndFileNewline.NoneFound from <ignore> and follow the same error-inspection and AI-fix steps.

Adding a trailing newline can change the end of the rendered HTML output, so update the Golden Master expectations:

Run `make php-test-golden-master-update`.


Code language: JavaScript (javascript)

When inspecting diffs or reviewing the PR, enable Hide whitespace on GitHub's Files changed tab—the added trailing newline shows up cleanly as + lines at the end of files.

3. Resolving Indent.DisallowTabIndent.TabsUsed (Golden Master Update Required)

Next, remove Indent.DisallowTabIndent.TabsUsed from <ignore> and repeat the flow: inspect errors, let the AI fix them, run make php-test-golden-master-update, then commit and open a PR.

Because indentation changes can affect rendered output, update the Golden Master baseline here as well. Enabling Hide whitespace in GitHub's Files changed view filters out pure indentation noise and makes it easy to spot any substantive differences.


Clearing Remaining Rules, Cleaning Up FIXME, and Switching to a Fixed Target

I cleared the remaining rules—such as Files.EndFileNewline.TooMany and Whitespace.SuperfluousWhitespace.EmptyLines—one by one using the same process.

I also kept Commenting.Fixme.TaskFound in <ignore>, though I resolved the FIXME items as well—including deleting some code that turned out to be unnecessary.

With the rule violations cleaned up, we could run smarty-lint against a fixed target (templates) rather than only changed files, so I simplified the GitHub Actions workflow accordingly:

    # ========================================
    # File Change Detection
    # ========================================
    # ========================================
    # Smarty Template Linting
    # ========================================
    - name: Run smarty-lint
      shell: bash
      run: composer run-script smarty-lint templates

By narrowing lint output one rule at a time via configuration and protecting output with Golden Master tests, I was able to maintain AI fix accuracy and establish a solid linting baseline across existing templates without large-scale rework.

GitHub - ninton/smartybook: Smarty動的Webサイト構築入門サンプルソース
Smarty動的Webサイト構築入門サンプルソース. Contribute to ninton/smartybook development by creating an account on GitHub.
タイトルとURLをコピーしました