PHPackages                             colinodell/indentation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. colinodell/indentation

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

colinodell/indentation
======================

Library to detect and manipulate indentation in strings and files

v1.0.0(4y ago)3911.5k↓42.2%3[1 PRs](https://github.com/colinodell/indentation/pulls)3MITPHPPHP ^8.0CI failing

Since Mar 16Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/colinodell/indentation)[ Packagist](https://packagist.org/packages/colinodell/indentation)[ Docs](https://github.com/colinodell/indentation)[ Fund](https://www.colinodell.com/sponsor)[ Fund](https://www.paypal.me/colinpodell/10.00)[ RSS](/packages/colinodell-indentation/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (7)Used By (3)

indentation
===========

[](#indentation)

[![Latest Version](https://camo.githubusercontent.com/c200a6c3e86c59c055c99e68e3c0610390e5700029e0332ec680d875ef8d8348/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6c696e6f64656c6c2f696e64656e746174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/colinodell/indentation)[![Total Downloads](https://camo.githubusercontent.com/4c567db323a207a05460e192eb02ca828e7a2ee429e5972682d0220f23a7bef1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6c696e6f64656c6c2f696e64656e746174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/colinodell/indentation)[![Software License](https://camo.githubusercontent.com/c2bffd81d308ced1cc3b0d66fb0ed453ab478a5e17c988b780f9de986a390ee2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://camo.githubusercontent.com/d1de9c23dab6d2fa22188bdf5fa9d3a661d8d705e0d9ab5b62fa2299fcdf8a4a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f636f6c696e6f64656c6c2f696e64656e746174696f6e2f54657374732f6d61696e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/colinodell/indentation/actions?query=workflow%3ATests+branch%3Amain)[![Coverage Status](https://camo.githubusercontent.com/09e28e0cd3818256553aae012efe514c101cb0592f97e74e63d600ba58b91dfd/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f636f6c696e6f64656c6c2f696e64656e746174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/colinodell/indentation/code-structure)[![Quality Score](https://camo.githubusercontent.com/9985393dc6bbd48562ec964d44785e0b622fea635753940f41e2cbe9974ba68e/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f636f6c696e6f64656c6c2f696e64656e746174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/colinodell/indentation)[![Psalm Type Coverage](https://camo.githubusercontent.com/b0550edaafad7a6a3814ae38d0b2238280d88feb25552559fa76b0d3589f8fc9/68747470733a2f2f73686570686572642e6465762f6769746875622f636f6c696e6f64656c6c2f696e64656e746174696f6e2f636f7665726167652e737667)](https://shepherd.dev/github/colinodell/indentation)[![Sponsor development of this project](https://camo.githubusercontent.com/2e662697b46a37233abdd7e45373397aab0bd5206336533151cdf42455d81048/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73706f6e736f72253230746869732532307061636b6167652d2545322539442541342d6666363962342e7376673f7374796c653d666c61742d737175617265)](https://www.colinodell.com/sponsor)

**PHP library to detect and manipulate the indentation of files and strings**

Installation
------------

[](#installation)

```
composer require --dev colinodell/indentation
```

Usage
-----

[](#usage)

### Detecting the indentation of a string or file

[](#detecting-the-indentation-of-a-string-or-file)

```
use ColinODell\Indentation\Indentation;

$indentation = Indentation::detect(file_get_contents('composer.json'));

assert($indentation->getAmount() === 4);
assert($indentation->getType() === Indentation::TYPE_SPACE);
assert((string)$indentation === '    ');
```

### Changing the indentation of a string or file

[](#changing-the-indentation-of-a-string-or-file)

```
use ColinODell\Indentation\Indentation;

$composerJson = file_get_contents('composer.json');
$composerJson = Indentation::change($composerJson, new Indentation(1, Indentation::TYPE_TAB));
file_put_contents('composer.json', $composerJson);
```

### Adding leading indentation to all lines

[](#adding-leading-indentation-to-all-lines)

Need to add indent all lines by some amount?

```
use ColinODell\Indentation\Indentation;

$codeExample = file_get_contents('file.php');
$indented = Indentation::indent($codeExample, new Indentation(4, Indentation::TYPE_SPACE));
```

Now you can embed the [indented code](https://spec.commonmark.org/0.30/#indented-code-blocks) into a Markdown document! (Hint: This works great with the [league/commonmark](https://commonmark.thephpleague.com/) library.)

### Removing leading indentation from all lines

[](#removing-leading-indentation-from-all-lines)

Imagine you have a file where every line is indented by at least 4 spaces:

```
    /**
     * Just a silly example
     */
    class Cat extends Animal
    {
        // ...
    }

```

You can trim that leading indentation while preserving the nested indentation with the `unindent()` method:

```
use ColinODell\Indentation\Indentation;

$contents = file_get_contents('Cat.php');
$trimmed = Indentation::unindent($contents);
file_put_contents('Cat.php', $trimmed);
```

Giving you:

```
/**
 * Just a silly example
 */
class Cat extends Animal
{
    // ...
}

```

Note how the leading 4 spaces are removed but all other indentation (like in the docblock and method body) is preserved.

Detection Algorithm
-------------------

[](#detection-algorithm)

The current algorithm looks for the most common difference between two consecutive non-empty lines.

In the following example, even if the 4-space indentation is used 3 times whereas the 2-space one is used 2 times, it is detected as less used because there were only 2 differences with this value instead of 4 for the 2-space indentation:

```
html {
  box-sizing: border-box;
}

body {
  background: gray;
}

p {
    line-height: 1.3em;
    margin-top: 1em;
    text-indent: 2em;
}
```

[Source.](https://medium.com/@heatherarthur/detecting-code-indentation-eff3ed0fb56b#3918)

Furthermore, if there are more than one most used difference, the indentation with the most lines is selected.

In the following example, the indentation is detected as 4-spaces:

```
body {
  background: gray;
}

p {
    line-height: 1.3em;
    margin-top: 1em;
    text-indent: 2em;
}
```

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance59

Moderate activity, may be stable

Popularity37

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

2

Last Release

1520d ago

Major Versions

v0.1.0 → v1.0.02022-03-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/4325b62a6ad366c731c3120595d861469be50f9da88df3ea99752c30ff98c179?d=identicon)[colinodell](/maintainers/colinodell)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (51 commits)")[![colinodell](https://avatars.githubusercontent.com/u/202034?v=4)](https://github.com/colinodell "colinodell (16 commits)")

---

Tags

filesstringsspacesindenttabsindentationdeindent

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/colinodell-indentation/health.svg)

```
[![Health](https://phpackages.com/badges/colinodell-indentation/health.svg)](https://phpackages.com/packages/colinodell-indentation)
```

###  Alternatives

[doctrine/inflector

PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words.

11.4k855.8M711](/packages/doctrine-inflector)[snipe/banbuilder

Library to easily handle profanity filtering in PHP projects.

3371.5M3](/packages/snipe-banbuilder)[michelf/php-smartypants

PHP SmartyPants

1115.3M30](/packages/michelf-php-smartypants)[lorisleiva/lody

Load files and classes as lazy collections in Laravel.

956.6M9](/packages/lorisleiva-lody)[gajus/dindent

HTML indentation library for development and testing.

1781.9M50](/packages/gajus-dindent)[kartik-v/bootstrap-tabs-x

Extended Bootstrap Tabs with ability to align tabs in multiple ways, add borders, rotated titles, and more.

1021.3M1](/packages/kartik-v-bootstrap-tabs-x)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
