-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: master
Are you sure you want to change the base?
Php diff 120 #128
Changes from 5 commits
996fbad
b1921ce
3d2ffe4
431d1c0
8f18b3f
f3fa77c
dcbb769
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
JBlond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function getLastLine() | ||
{ | ||
$lines = $this->file; | ||
return end($lines); | ||
JBlond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* 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; | ||
JBlond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* 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)"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double quotes can be replaced by single quotes There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double quotes can be replaced by single quotes There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double quotes can be replaced by single quotes There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
JBlond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum |
Uh oh!
There was an error while loading. Please reload this page.