PHPackages                             ptlis/diff-parser - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. ptlis/diff-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

ptlis/diff-parser
=================

A parser for unified diff files, returning a hydrated object graph.

1.1.0(3y ago)29178.1k↑10.4%37MITPHPPHP &gt;=8.1.0

Since Jan 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/ptlis/diff-parser)[ Packagist](https://packagist.org/packages/ptlis/diff-parser)[ RSS](/packages/ptlis-diff-parser/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (15)Used By (7)

ptlis/diff-parser
=================

[](#ptlisdiff-parser)

A parser for unified diff files, returning a hydrated object graph.

Uses \_\_toString() to serialize back into unified diff format.

[![CircleCI](https://camo.githubusercontent.com/bb575d192778514399de95942833d0ba922125187bf07121bd0c12ae7365976c/68747470733a2f2f646c2e636972636c6563692e636f6d2f7374617475732d62616467652f696d672f67682f70746c69732f646966662d7061727365722f747265652f6d61696e2e7376673f7374796c653d737667)](https://dl.circleci.com/status-badge/redirect/gh/ptlis/diff-parser/tree/main) [![codecov](https://camo.githubusercontent.com/6c1e796fe06903b01545a38065731d9ed96a9b29ab2cab465cfbdf47490403b4/68747470733a2f2f636f6465636f762e696f2f67682f70746c69732f646966662d7061727365722f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d72384e676a5a7956564c)](https://codecov.io/gh/ptlis/diff-parser) [![Latest Stable Version](https://camo.githubusercontent.com/007e27484a565d771cf3a51e44f558b0e7a8b9918aabe694b016d400aca86598/68747470733a2f2f706f7365722e707567782e6f72672f70746c69732f646966662d7061727365722f762f737461626c652e706e67)](https://packagist.org/packages/ptlis/diff-parser)

Install
-------

[](#install)

Install with composer:

```
$ composer require ptlis/diff-parser
```

Usage
-----

[](#usage)

### Parsing a diff file

[](#parsing-a-diff-file)

Create a parser:

```
$parser = new \ptlis\DiffParser\Parser();
```

And then get a changeset from a file path:

```
$changeset = $parser->parseFile('path/to/git/diff', Parser::VCS_GIT);
```

or parse the patch data stored from a variable:

```
$changeset = $parser->parse($patchData, Parser::VCS_SVN);
```

### Serialization

[](#serialization)

All the value classes implement the `__toString()` method to support direct serialization of that component back to unified diff format.

For example this serializes the data in `$changeset` into the file `my.patch`.

```
\file_put_contents('my.patch', $changeset);
```

### The Object Graph

[](#the-object-graph)

The tree built to store changesets is very simple, mapping one-to-one to the components of a diff file. In essence:

- A Changeset is the root node &amp; contains Files
- A File contain Hunks
- A Hunk contain Lines
- Lines are the leaf nodes.

#### Changeset

[](#changeset)

From a Changeset you may iterate over the array of files that have changed:

```
foreach ($changeset->files as $file) {
    // $file is an instance of ptlis\DiffParser\File
}
```

#### File

[](#file)

Get the original and new filenames:

```
$file->filename->original;  // Eg 'readme.md' or '' (empty) on create
$file->filename->new;       // EG 'README.md' or '' (empty) on delete
```

Get the operation that was performed (create, delete or change):

```
$file->operation;   // One of File::CREATED, File::CHANGED, File::DELETED
```

From a file you may iterate over the change hunks:

```
foreach ($file->hunks as $hunk) {
    // $hunk is an instance of ptlis\DiffParser\Hunk
}
```

#### Hunk

[](#hunk)

Get the start line number of the hunk:

```
$hunk->startLine->original; // Eg '0'
$hunk->startLine->new;      // Eg '0'
```

Get the number of lines affected in the hunk:

```
$hunk->affectedLines->original; // Eg '5'
$hunk->affectedLines->new;      // Eg '7'
```

From a hunk you may iterate over the changed lines:

```
foreach ($hunk->lines as $line) {
    // $line is an instance of ptlis\DiffParser\Line
}
```

#### Line

[](#line)

Get the original and new line numbers:

```
$line->number->original;    // Eg '7' or '-1' on create
$line->number->new;         // Eg '7' or '-1' on delete
```

Get the operation:

```
$line->operation;   // One of Line::ADDED, Line::REMOVED, Line::UNCHANGED
```

Get the value of the line:

```
$line->content; // Eg ' $foo = bar;'
```

Contributing
------------

[](#contributing)

You can contribute by submitting an Issue to the [issue tracker](https://github.com/ptlis/vcs/issues), improving the documentation or submitting a pull request. For pull requests i'd prefer that the code style and test coverage is maintained, but I am happy to work through any minor issues that may arise so that the request can be merged.

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 96.2% 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 ~297 days

Recently: every ~478 days

Total

10

Last Release

1457d ago

Major Versions

v0.6.0 → 1.0.02020-10-31

PHP version history (4 changes)v0.1.0PHP &gt;=5.3.0

1.0.0PHP ^7.2.0

1.0.1PHP &gt;=7.2.0

1.1.0PHP &gt;=8.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7ff8b14a43509e32a3892de4a8907b3b7764a6f7910454ae3f0f03e2541eea5c?d=identicon)[ptlis](/maintainers/ptlis)

---

Top Contributors

[![ptlis](https://avatars.githubusercontent.com/u/508422?v=4)](https://github.com/ptlis "ptlis (76 commits)")[![anomiex](https://avatars.githubusercontent.com/u/1030580?v=4)](https://github.com/anomiex "anomiex (1 commits)")[![mre](https://avatars.githubusercontent.com/u/175809?v=4)](https://github.com/mre "mre (1 commits)")[![winternet-studio](https://avatars.githubusercontent.com/u/5200270?v=4)](https://github.com/winternet-studio "winternet-studio (1 commits)")

---

Tags

diffdiff-parserphpunifiedunified-diffsdiffunified diffparser

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ptlis-diff-parser/health.svg)

```
[![Health](https://phpackages.com/badges/ptlis-diff-parser/health.svg)](https://phpackages.com/packages/ptlis-diff-parser)
```

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k902.6M1.8k](/packages/nikic-php-parser)[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.0k151.8M732](/packages/erusev-parsedown)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

3.0k404.0M702](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)

PHPackages © 2026

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