sebastianbergmann / diff

Diff implementation
BSD 3-Clause "New" or "Revised" License
7.58k stars 85 forks source link

Numeric array values not supported #109

Closed tgr closed 1 year ago

tgr commented 1 year ago

Tested on latest main:

Psy Shell v0.11.9 (PHP 8.1.6 — cli) by Justin Hileman
> $ob = new SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder
= SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder {#2715}

> (new SebastianBergmann\Diff\Differ($ob))->diff([1], [2])
   TypeError  substr(): Argument #1 ($string) must be of type string, int given.

> wtf -a
   TypeError  substr(): Argument #1 ($string) must be of type string, int given.
--
 0:  () at vendor/sebastian/diff/src/Output/UnifiedDiffOutputBuilder.php:98
 1:  substr() at vendor/sebastian/diff/src/Output/UnifiedDiffOutputBuilder.php:98
 2:  SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder->writeDiffHunks() at vendor/sebastian/diff/src/Output/UnifiedDiffOutputBuilder.php:62
 3:  SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder->getDiff() at vendor/sebastian/diff/src/Differ.php:54
 4:  SebastianBergmann\Diff\Differ->diff() at eval()'d code:1
 5:  eval() at vendor/psy/psysh/src/ExecutionLoopClosure.php:53
 6:  Psy\{closure}() at vendor/psy/psysh/src/ExecutionClosure.php:89
 7:  Psy\ExecutionClosure->execute() at vendor/psy/psysh/src/Shell.php:394
 8:  Psy\Shell->doInteractiveRun() at vendor/psy/psysh/src/Shell.php:365
 9:  Psy\Shell->doRun() at vendor/symfony/console/Application.php:168
10:  Symfony\Component\Console\Application->run() at vendor/psy/psysh/src/Shell.php:340
11:  Psy\Shell->run() at vendor/psy/psysh/src/functions.php:454
12:  Psy\{closure}() at vendor/psy/psysh/bin/psysh:148
13:  include() at vendor/bin/psysh:117

Not sure if this is a bug as the documentation doesn't say anything about diffing arrays, but the function signature suggests it's supported and PHPUnit uses it. The same command works fine with string values:

> (new SebastianBergmann\Diff\Differ($ob))->diff(['1'], ['2'])
= """
  --- Original\n
  +++ New\n
  @@ @@\n
  -1\n
  +2\n
  """
tgr commented 1 year ago

(Also, FWIW, the documentation says you can create a Differ with no argument, but it requires an output builder now.)

SpacePossum commented 1 year ago

Hi!

Can you point out where PHPUnit uses the diff function with something other than strings or string-arrays? I cannot recall that ever being supported as the package builds output in udiff format which always apply to strings inputs only (AFAIK).

I also wonder what the expected output would be of (new SebastianBergmann\Diff\Differ($ob))->diff([1], [2]) (other than maybe a better exception)?

sebastianbergmann commented 1 year ago

No feedback, closing.