Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lolli 14 #14

Merged
merged 8 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'declare_strict_types' => true,
'dir_constant' => true,
'function_typehint_space' => true,
'header_comment' => [
'header' => 'FINE granularity DIFF'
. chr(10) . chr(10) . '(c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)'
. chr(10) . '(c) 2013 Robert Crowe (http://cogpowered.com)'
. chr(10) . '(c) 2021 Christian Kuhn'
. chr(10). chr(10) . 'For the full copyright and license information, please view'
. chr(10) . 'the LICENSE file that was distributed with this source code.',
],
'lowercase_cast' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'modernize_types_casting' => true,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "library",
"require": {
"php": ">=7.2.0",
"ext-mbstring": "*"
"symfony/polyfill-mbstring": "^1.23"
},
"require-dev": {
"phpunit/phpunit": "^8 || ^9",
Expand Down
8 changes: 7 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
parameters:
level: 5
level: 8

paths:
- ./src
- ./tests

ignoreErrors:
-
message: "#^Parameter \\#1 \\$opcodes of method cogpowered\\\\FineDiff\\\\Parser\\\\Opcodes\\:\\:setOpcodes\\(\\) expects array\\<int, cogpowered\\\\FineDiff\\\\Parser\\\\Operations\\\\OperationInterface\\>, array\\<int, string\\> given\\.$#"
count: 1
path: tests/Parser/OpcodesTest.php
19 changes: 8 additions & 11 deletions src/Delimiters.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

/**
declare(strict_types=1);

/*
* FINE granularity DIFF
*
* Computes a set of instructions to convert the content of
* one string into another.
*
* Originally created by Raymond Hill (https://github.com/gorhill/PHP-FineDiff), brought up
* to date by Cog Powered (https://github.com/cogpowered/FineDiff).
* (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
* (c) 2013 Robert Crowe (http://cogpowered.com)
* (c) 2021 Christian Kuhn
*
* @copyright Copyright 2011 (c) Raymond Hill (http://raymondhill.net/blog/?p=441)
* @copyright Copyright 2013 (c) Robert Crowe (http://cogpowered.com)
* @link https://github.com/cogpowered/FineDiff
* @version 0.0.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace cogpowered\FineDiff;
Expand Down
63 changes: 19 additions & 44 deletions src/Diff.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
<?php

/**
declare(strict_types=1);

/*
* FINE granularity DIFF
*
* Computes a set of instructions to convert the content of
* one string into another.
* (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
* (c) 2013 Robert Crowe (http://cogpowered.com)
* (c) 2021 Christian Kuhn
*
* Originally created by Raymond Hill (https://github.com/gorhill/PHP-FineDiff), brought up
* to date by Cog Powered (https://github.com/cogpowered/FineDiff).
*
* @copyright Copyright 2011 (c) Raymond Hill (http://raymondhill.net/blog/?p=441)
* @copyright Copyright 2013 (c) Robert Crowe (http://cogpowered.com)
* @link https://github.com/cogpowered/FineDiff
* @version 0.0.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace cogpowered\FineDiff;

use cogpowered\FineDiff\Granularity\Character;
use cogpowered\FineDiff\Granularity\GranularityInterface;
use cogpowered\FineDiff\Parser\Opcodes;
use cogpowered\FineDiff\Parser\OpcodesInterface;
use cogpowered\FineDiff\Parser\Parser;
use cogpowered\FineDiff\Parser\ParserInterface;
Expand Down Expand Up @@ -56,99 +52,78 @@ class Diff
*/
public function __construct(GranularityInterface $granularity = null, RendererInterface $renderer = null, ParserInterface $parser = null)
{
// Set some sensible defaults

// Set the granularity of the diff
$this->granularity = $granularity ?? new Character();

// Set the renderer to use when calling Diff::render
$this->renderer = $renderer ?? new Html();

// Set the diff parser
$this->parser = $parser ?? new Parser($this->granularity);
}

/**
* Returns the granularity object used by the parser.
*
* @return GranularityInterface
*/
public function getGranularity()
public function getGranularity(): GranularityInterface
{
return $this->parser->getGranularity();
}

/**
* Set the granularity level of the parser.
*/
public function setGranularity(GranularityInterface $granularity)
public function setGranularity(GranularityInterface $granularity): void
{
$this->parser->setGranularity($granularity);
}

/**
* Get the render.
*
* @return RendererInterface
*/
public function getRenderer()
public function getRenderer(): RendererInterface
{
return $this->renderer;
}

/**
* Set the renderer.
*/
public function setRenderer(RendererInterface $renderer)
public function setRenderer(RendererInterface $renderer): void
{
$this->renderer = $renderer;
}

/**
* Get the parser responsible for generating the diff/opcodes.
*
* @return ParserInterface
*/
public function getParser()
public function getParser(): ParserInterface
{
return $this->parser;
}

/**
* Set the parser.
*/
public function setParser(ParserInterface $parser)
public function setParser(ParserInterface $parser): void
{
$this->parser = $parser;
}

/**
* Gets the diff / opcodes between two strings.
*
* Returns the opcode diff which can be used for example, to
* to generate a HTML report of the differences.
*
* @return OpcodesInterface
* Returns the opcode diff which can be used for example
* to generate HTML report of the differences.
*/
public function getOpcodes($from_text, $to_text)
public function getOpcodes(string $from_text, string $to_text): OpcodesInterface
{
return $this->parser->parse($from_text, $to_text);
}

/**
* Render the difference between two strings.
*
* By default will return the difference as HTML.
*
* @param string $from_text
* @param string $to_text
* @return string
* By default, will return the difference as HTML.
*/
public function render($from_text, $to_text)
public function render(string $from_text, string $to_text): string
{
// First we need the opcodes
$opcodes = $this->getOpcodes($from_text, $to_text);

return $this->renderer->process($from_text, $opcodes);
}
}
27 changes: 12 additions & 15 deletions src/Exceptions/GranularityCountException.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<?php

/**
* FINE granularity DIFF
*
* Computes a set of instructions to convert the content of
* one string into another.
*
* Originally created by Raymond Hill (github.com/gorhill/PHP-FineDiff), brought up
* to date by Cog Powered (github.com/cogpowered/FineDiff).
*
* @copyright Copyright 2011 (c) Raymond Hill (http://raymondhill.net/blog/?p=441)
* @copyright Copyright 2013 (c) Robert Crowe (http://cogpowered.com)
* @link https://github.com/cogpowered/FineDiff
* @version 0.0.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
declare(strict_types=1);

/*
* FINE granularity DIFF
*
* (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
* (c) 2013 Robert Crowe (http://cogpowered.com)
* (c) 2021 Christian Kuhn
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace cogpowered\FineDiff\Exceptions;

Expand Down
19 changes: 8 additions & 11 deletions src/Exceptions/OperationException.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

/**
declare(strict_types=1);

/*
* FINE granularity DIFF
*
* Computes a set of instructions to convert the content of
* one string into another.
*
* Originally created by Raymond Hill (https://github.com/gorhill/PHP-FineDiff), brought up
* to date by Cog Powered (https://github.com/cogpowered/FineDiff).
* (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
* (c) 2013 Robert Crowe (http://cogpowered.com)
* (c) 2021 Christian Kuhn
*
* @copyright Copyright 2011 (c) Raymond Hill (http://raymondhill.net/blog/?p=441)
* @copyright Copyright 2013 (c) Robert Crowe (http://cogpowered.com)
* @link https://github.com/cogpowered/FineDiff
* @version 0.0.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace cogpowered\FineDiff\Exceptions;
Expand Down
22 changes: 11 additions & 11 deletions src/Granularity/Character.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

/**
declare(strict_types=1);

/*
* FINE granularity DIFF
*
* Computes a set of instructions to convert the content of
* one string into another.
*
* Originally created by Raymond Hill (https://github.com/gorhill/PHP-FineDiff), brought up
* to date by Cog Powered (https://github.com/cogpowered/FineDiff).
* (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
* (c) 2013 Robert Crowe (http://cogpowered.com)
* (c) 2021 Christian Kuhn
*
* @copyright Copyright 2011 (c) Raymond Hill (http://raymondhill.net/blog/?p=441)
* @copyright Copyright 2013 (c) Robert Crowe (http://cogpowered.com)
* @link https://github.com/cogpowered/FineDiff
* @version 0.0.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace cogpowered\FineDiff\Granularity;
Expand All @@ -25,6 +22,9 @@
*/
class Character extends Granularity
{
/**
* @var array<int, string>
*/
protected $delimiters = [
Delimiters::PARAGRAPH,
Delimiters::SENTENCE,
Expand Down
27 changes: 12 additions & 15 deletions src/Granularity/Granularity.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

/**
declare(strict_types=1);

/*
* FINE granularity DIFF
*
* Computes a set of instructions to convert the content of
* one string into another.
*
* Originally created by Raymond Hill (https://github.com/gorhill/PHP-FineDiff), brought up
* to date by Cog Powered (https://github.com/cogpowered/FineDiff).
* (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
* (c) 2013 Robert Crowe (http://cogpowered.com)
* (c) 2021 Christian Kuhn
*
* @copyright Copyright 2011 (c) Raymond Hill (http://raymondhill.net/blog/?p=441)
* @copyright Copyright 2013 (c) Robert Crowe (http://cogpowered.com)
* @link https://github.com/cogpowered/FineDiff
* @version 0.0.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace cogpowered\FineDiff\Granularity;
Expand All @@ -24,7 +21,7 @@
abstract class Granularity implements GranularityInterface
{
/**
* @var array Extending granularities should override this.
* @var array<int, string> Extending granularities should override this.
*/
protected $delimiters = [];

Expand All @@ -43,7 +40,7 @@ public function offsetExists($offset)
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->delimiters[$offset]) ? $this->delimiters[$offset] : null;
return $this->delimiters[$offset] ?? null;
}

/**
Expand Down Expand Up @@ -82,15 +79,15 @@ public function count()
/**
* @inheritdoc
*/
public function getDelimiters()
public function getDelimiters(): array
{
return $this->delimiters;
}

/**
* @inheritdoc
*/
public function setDelimiters(array $delimiters)
public function setDelimiters(array $delimiters): void
{
$this->delimiters = $delimiters;
}
Expand Down
Loading