Skip to content

Php diff 120 #128

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
phpcs.xml export-ignore
tests/resources/eol/*.txt binary
84 changes: 84 additions & 0 deletions lib/jblond/Diff/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace jblond\Diff;

use InvalidArgumentException;

/**
*
*/
class File
{
/** @var array $file */
protected $file;


/**
* Add data
* @param string|array $file
* @return void
*/
public function setFile($file): void
{
if (is_array($file)) {
$this->file = $file;
return;
}
if (!file_exists($file)) {
throw new InvalidArgumentException();
}
$this->file = file($file);
}

/**
* return the last line from the file array
* @return false|mixed|string
*/
public function getLastLine()
{
$lines = $this->file;
return end($lines);
}

/**
* Bool return if the file has a new line at the end
* @return bool
*/
public function hasNewLineAtTheEnd(): bool
{
$lastLine = $this->getLastLine();
if (strpos($lastLine, "\r\n") !== false) {
return true;
}

if (strpos($lastLine, "\r") !== false) {
return true;
}

if (strpos($lastLine, "\n") !== false) {
return true;
}
return false;
}

/**
* Return the File Ending / EOL / EOF Type
* @return string
*/
public function getEOLType(): string
{
$lastLine = $this->getLastLine();
if (strpos($lastLine, "\r\n") !== false) {
return "EOL type is Windows (CRLF)";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double quotes can be replaced by single quotes

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can't be replaced. if I replace it with single quotes the unit test fails.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is referring to line 72, not 71.

}

if (strpos($lastLine, "\r") !== false) {
return "EOL type is Mac (CR)";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double quotes can be replaced by single quotes

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can't be replaced. if I replace it with single quotes the unit test fails.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is referring to line 76, not 75.

}

if (strpos($lastLine, "\n") !== false) {
return "EOL type is Unix (LF)";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double quotes can be replaced by single quotes

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can't be replaced. if I replace it with single quotes the unit test fails.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is referring to line 80, not 79.

}
return "\ No newline at end of file";
}
}
146 changes: 146 additions & 0 deletions tests/Diff/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

namespace Tests\Diff;

use InvalidArgumentException;
use jblond\Diff\File;
use PHPUnit\Framework\TestCase;

/**
*
*/
class FileTest extends TestCase
{
/**
* Test the type of EOF
*/
public function testGetEOLType()
{
$mac = new File();
$mac->setFile('tests/resources/eol/mac.txt');
$this->assertEquals(
'EOL type is Mac (CR)',
$mac->getEOLType()
);

$unix = new File();
$unix->setFile('tests/resources/eol/unix.txt');
$this->assertEquals(
'EOL type is Unix (LF)',
$unix->getEOLType()
);

$noEol = new File();
$noEol->setFile('tests/resources/eol/no-eol.txt');
$this->assertEquals(
'\ No newline at end of file',
$noEol->getEOLType()
);

$windows = new File();
$windows->setFile('tests/resources/eol/windows.txt');
$this->assertEquals(
'EOL type is Windows (CRLF)',
$windows->getEOLType()
);

$a = new File();
$a->setFile('tests/resources/a.txt');
$this->assertEquals(
'EOL type is Unix (LF)',
$a->getEOLType()
);
}

/**
* Bool test if the file has a line ending
*/
public function testHasNewLineAtTheEnd()
{
$mac = new File();
$mac->setFile('tests/resources/eol/mac.txt');
$this->assertEquals(
true,
$mac->hasNewLineAtTheEnd()
);

$unix = new File();
$unix->setFile('tests/resources/eol/unix.txt');
$this->assertEquals(
true,
$unix->hasNewLineAtTheEnd()
);

$noEol = new File();
$noEol->setFile('tests/resources/eol/no-eol.txt');
$this->assertEquals(
false,
$noEol->hasNewLineAtTheEnd()
);

$windows = new File();
$windows->setFile('tests/resources/eol/windows.txt');
$this->assertEquals(
true,
$windows->hasNewLineAtTheEnd()
);

$a = new File();
$a->setFile('tests/resources/a.txt');
$this->assertEquals(
true,
$a->hasNewLineAtTheEnd()
);

$b = new File();
$b->setFile('tests/resources/b.txt');
$this->assertEquals(
true,
$b->hasNewLineAtTheEnd()
);
}

/**
* Test get the last string from a file
*/
public function testGetLastLine()
{
$mac = new File();
$mac->setFile('tests/resources/eol/mac.txt');
$this->assertEquals(
"Lorem ipsum\r",
$mac->getLastLine()
);

$unix = new File();
$unix->setFile('tests/resources/eol/unix.txt');
$this->assertEquals(
"Lorem ipsum\n",
$unix->getLastLine()
);

$noEol = new File();
$noEol->setFile('tests/resources/eol/no-eol.txt');
$this->assertEquals(
"Lorem ipsum",
$noEol->getLastLine()
);

$windows = new File();
$windows->setFile('tests/resources/eol/windows.txt');
$this->assertEquals(
"Lorem ipsum\r\n",
$windows->getLastLine()
);
}

/**
* Test if the file exists
*/
public function testSetFile()
{
$this->expectException(InvalidArgumentException::class);
$file = new File();
$file->setFile('foo.txt');
}
}
1 change: 1 addition & 0 deletions tests/resources/eol/mac.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum
1 change: 1 addition & 0 deletions tests/resources/eol/no-eol.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum
1 change: 1 addition & 0 deletions tests/resources/eol/unix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum
1 change: 1 addition & 0 deletions tests/resources/eol/windows.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum