{
    "slug": "inconsistent_indentation_width",
    "term": "Inconsistent Indentation",
    "category": "style",
    "difficulty": "beginner",
    "short": "Mixing tabs and spaces, or using varying numbers of spaces for indentation — causes visual misalignment across editors and makes diffs noisy.",
    "long": "PSR-1/PSR-12 mandates 4 spaces per indent level, no tabs. Inconsistent indentation typically enters codebases when developers with different editor settings commit without formatting, or when code is pasted from an external source. .editorconfig and PHP-CS-Fixer enforce consistent indentation automatically. Mixed tabs and spaces are the worst offender — the visual appearance depends entirely on the editor's tab-width setting.",
    "aliases": [
        "mixed tabs spaces",
        "indentation",
        "tab width"
    ],
    "tags": [
        "style",
        "php",
        "formatting",
        "psr-12"
    ],
    "misconception": "Tabs are better than spaces because they are configurable — configurability is the problem: code with tabs looks different in every editor, breaking visual alignment of code reviewed by a team.",
    "why_it_matters": "Mixed indentation turns every line of a reformatted file into a diff change, obscuring the actual logic changes in code review — automated enforcement eliminates this entirely.",
    "common_mistakes": [
        "Pasting code from Stack Overflow or documentation that uses 2-space or tab indentation.",
        "Editor auto-indent set to tabs in a spaces project.",
        "Using 2 spaces for nested closures 'to save horizontal space'.",
        "Not having .editorconfig in the repository to enforce consistent settings across all editors."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "editorconfig",
        "php_cs_fixer",
        "psr_12"
    ],
    "prerequisites": [
        "psr_12",
        "editorconfig",
        "php_cs_fixer"
    ],
    "refs": [
        "https://www.php-fig.org/psr/psr-12/#24-lines"
    ],
    "bad_code": "<?php\nfunction calculate(int $x): int\n{\n\t$result = 0;        // Tab\n    if ($x > 0) {     // 4 spaces\n      $result = $x;   // 2 spaces — 3 different styles in 4 lines\n    }\n    return $result;\n}",
    "good_code": "// .editorconfig in project root:\n[*.php]\nindent_style = space\nindent_size = 4\n\n// .php-cs-fixer.dist.php:\n'indentation_type' => true,  // Enforces 4-space indentation\n\n<?php\nfunction calculate(int $x): int\n{\n    $result = 0;\n    if ($x > 0) {\n        $result = $x;\n    }\n    return $result;\n}",
    "quick_fix": "Standardise on 4 spaces (PSR-12) and enforce with php-cs-fixer and .editorconfig — mixed indentation is invisible in some editors and breaks diffs",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/inconsistent_indentation_width",
        "html_url": "https://codeclaritylab.com/glossary/inconsistent_indentation_width",
        "json_url": "https://codeclaritylab.com/glossary/inconsistent_indentation_width.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Inconsistent Indentation](https://codeclaritylab.com/glossary/inconsistent_indentation_width) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/inconsistent_indentation_width"
            }
        }
    }
}