Skip to content

Commit 2d85c50

Browse files
committed
Init with default logic
0 parents  commit 2d85c50

14 files changed

+321
-0
lines changed

.github/ISSUE_TEMPLATE/Bug_report.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: "🐛 Bug Report"
3+
about: "If something isn't working as expected 🤔"
4+
5+
---
6+
7+
Version: ?.?.?
8+
9+
### Bug Description
10+
... A clear and concise description of what the bug is. A good bug report shouldn't leave others needing to chase you up for more information.
11+
12+
### Steps To Reproduce
13+
... If possible a minimal demo of the problem ...
14+
15+
### Expected Behavior
16+
... A clear and concise description of what you expected to happen.
17+
18+
### Possible Solution
19+
... Only if you have suggestions on a fix for the bug
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
name: "🚀 Feature Request"
3+
about: "I have a suggestion (and may want to implement it) 🙂"
4+
5+
---
6+
7+
- Is your feature request related to a problem? Please describe.
8+
- Explain your intentions.
9+
- It's up to you to make a strong case to convince the project's developers of the merits of this feature.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
name: "🤗 Support Question"
3+
about: "If you have a question 💬"
4+
5+
---
6+
7+
--------------^ Click "Preview" for a nicer view!
8+
We primarily use GitHub as an issue tracker; for usage and support questions, please check out https://php.baraja.cz. Thanks! 😁.

.github/funding.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: janbarasek
2+
custom: ["https://baraja.cz", "https://php.baraja.cz", "https://vue.baraja.cz"]

.github/pull_request_template.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- bug fix / new feature? <!-- #issue numbers, if any -->
2+
- BC break? yes/no
3+
4+
<!--
5+
Describe your changes here to communicate to the maintainers why we should accept this pull request.
6+
7+
Please add new tests to show the fix or feature works.
8+
9+
Thanks for contributing to Baraja opensource ecosystem!
10+
-->

.github/workflows/coding-style.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Coding Style
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
nette_cc:
7+
name: Nette Code Checker
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: shivammathur/setup-php@v2
12+
with:
13+
php-version: 8.0
14+
coverage: none
15+
16+
- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
17+
- run: php temp/code-checker/code-checker --strict-types --no-progress
18+
19+
nette_cs:
20+
name: Nette Coding Standard
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v2
24+
- uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: 8.0
27+
coverage: none
28+
29+
- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress --ignore-platform-reqs
30+
- run: php temp/coding-standard/ecs check src

.github/workflows/main.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Integrity check
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
types: [ assigned, opened, synchronize, reopened ]
9+
schedule:
10+
- cron: '1 * * * *'
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@master
18+
19+
- name: Install PHP
20+
uses: shivammathur/setup-php@master
21+
with:
22+
php-version: 8.0
23+
24+
- name: Install composer deps
25+
run: |
26+
composer create-project nette/code-checker temp/code-checker ^3 --no-progress
27+
composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress
28+
29+
# Install app deps
30+
composer install --no-interaction --prefer-dist
31+
32+
- name: The PHP Security Checker
33+
uses: symfonycorp/security-checker-action@v2
34+
35+
- name: Check PHPStan rules
36+
run: composer phpstan

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Baraja packages
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Simple PHP diff
2+
===============
3+
4+
Find the quick difference between two text files in PHP.
5+
6+
Example
7+
-------
8+
9+
![Default theme](doc/simple-diff.png)
10+
11+
Idea
12+
----
13+
14+
The library compares two text files very quickly and returns the object with the differences.
15+
16+
The difference has numbered lines for easy display of changes to the user. You can also read an array of changed rows as an integer array from the `Diff` object as you browse for changes.
17+
18+
How to use
19+
----------
20+
21+
Simply create a SimpleDiff instance and compare the two files:
22+
23+
```php
24+
$left = 'First text';
25+
$right = 'Second text';
26+
27+
$diff = (new \Baraja\DiffGenerator\SimpleDiff)->compare($left, $right);
28+
29+
// simple render diff
30+
echo '<code><pre>'
31+
. htmlspecialchars((string) $diff)
32+
. '</pre></code>';
33+
```
34+
35+
The `compare()` method returns a complete object `Diff` with the results of the comparison, from which you can get much more.
36+
37+
For example, to get a list of changed rows:
38+
39+
```php
40+
echo 'Changed lines: ';
41+
echo implode(', ', $diff->getChangedLines());
42+
```
43+
44+
Comparison mode
45+
---------------
46+
47+
This tool supports strict and basic comparison modes (strict mode is disabled by default).
48+
Strict mode also allows you to compare changes in different line wrapping methods (for example, `"\n"` and so on).

composer.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "baraja-core/simple-php-diff",
3+
"description": "Find the quick difference between two text files in PHP.",
4+
"homepage": "https://github.com/baraja-core/simple-php-diff",
5+
"authors": [
6+
{
7+
"name": "Jan Barášek",
8+
"homepage": "https://php.baraja.cz"
9+
}
10+
],
11+
"require": {
12+
"php": "^8.0"
13+
},
14+
"require-dev": {
15+
"phpstan/phpstan": "^0.12.74",
16+
"tracy/tracy": "^2.8",
17+
"phpstan/phpstan-nette": "^0.12.14",
18+
"spaze/phpstan-disallowed-calls": "^1.1",
19+
"roave/security-advisories": "dev-master",
20+
"jetbrains/phpstorm-attributes": "^1.0"
21+
},
22+
"autoload": {
23+
"classmap": [
24+
"src/"
25+
]
26+
},
27+
"scripts": {
28+
"phpstan": [
29+
"vendor/bin/phpstan analyse src -c phpstan.neon --level 8 --no-progress"
30+
]
31+
},
32+
"minimum-stability": "stable"
33+
}

doc/simple-diff.png

60.5 KB
Loading

phpstan.neon

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- vendor/phpstan/phpstan-nette/extension.neon
3+
- vendor/phpstan/phpstan-nette/rules.neon
4+
- vendor/spaze/phpstan-disallowed-calls/extension.neon
5+
- vendor/spaze/phpstan-disallowed-calls/disallowed-dangerous-calls.neon
6+
- vendor/spaze/phpstan-disallowed-calls/disallowed-execution-calls.neon
7+
8+
parameters:
9+
checkMissingIterableValueType: false

src/Diff.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baraja\DiffGenerator;
6+
7+
8+
final class Diff
9+
{
10+
/**
11+
* @param int[] $changedLines
12+
*/
13+
public function __construct(
14+
private string $original,
15+
private string $target,
16+
private string $diff,
17+
private array $changedLines,
18+
) {
19+
}
20+
21+
22+
public function __toString(): string
23+
{
24+
return $this->diff;
25+
}
26+
27+
28+
public function getOriginal(): string
29+
{
30+
return $this->original;
31+
}
32+
33+
34+
public function getTarget(): string
35+
{
36+
return $this->target;
37+
}
38+
39+
40+
public function getDiff(): string
41+
{
42+
return $this->diff;
43+
}
44+
45+
46+
/**
47+
* @return int[]
48+
*/
49+
public function getChangedLines(): array
50+
{
51+
return $this->changedLines;
52+
}
53+
}

src/SimpleDiff.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baraja\DiffGenerator;
6+
7+
8+
final class SimpleDiff
9+
{
10+
public function compare(string $left, string $right, bool $strict = false): Diff
11+
{
12+
if ($strict === false) {
13+
$left = str_replace(["\r\n", "\r"], "\n", trim($left));
14+
$right = str_replace(["\r\n", "\r"], "\n", trim($right));
15+
}
16+
17+
$return = [];
18+
$from = explode("\n", $left);
19+
$to = explode("\n", $right);
20+
$changedLines = [];
21+
22+
for ($i = 0; isset($from[$i]); $i++) {
23+
$original = $from[$i] ?? '';
24+
$target = $to[$i] ?? '';
25+
$lineNumber = str_pad(($i + 1) . ':', 6, ' ') . ' ';
26+
if ($original === $target) {
27+
$return[] = $lineNumber . ' ' . $original;
28+
} else {
29+
$return[] = '- ' . $lineNumber . $this->prettyRender($original);
30+
$return[] = '+ ' . $lineNumber . $this->prettyRender($target);
31+
$changedLines[] = $i + 1;
32+
}
33+
}
34+
35+
return new Diff($left, $right, implode("\n", $return), $changedLines);
36+
}
37+
38+
39+
private function prettyRender(string $haystack): string
40+
{
41+
return str_replace(["\t", ' '], ['→→→→', '·'], $haystack);
42+
}
43+
}

0 commit comments

Comments
 (0)