Skip to content

Commit

Permalink
Merge pull request #16 from mediact/hotfix/issue-15
Browse files Browse the repository at this point in the history
1.0.3 - Fix fread call when file size is zero
  • Loading branch information
janmartenjongerius authored Jul 10, 2018
2 parents b3c77d4 + afbe670 commit e5e6eeb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Php/SymbolExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ public function extract(

foreach ($files as $file) {
try {
$size = $file->getSize();
$handle = $file->openFile('rb');
$contents = $handle->fread($file->getSize());
$contents = $size > 0 ? $handle->fread($size) : '';

if (!$contents) { // phpstan thinks this can only return string.
if (empty($contents)) {
continue;
}

Expand Down
16 changes: 11 additions & 5 deletions tests/Php/SymbolExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testConstructor(): void

/**
* @dataProvider emptyProvider
* @dataProvider unparsableFilesProvider
* @dataProvider nonParsingFilesProvider
* @dataProvider filledFilesProvider
*
* @param Parser $parser
Expand Down Expand Up @@ -115,14 +115,20 @@ public function emptyProvider(): array
->with(self::anything());

return [
[$parser, $this->createFileIterator()]
[$parser, $this->createFileIterator()],
[
$parser,
$this->createFileIterator(
$this->createFile('')
)
]
];
}

/**
* @param string $content
*
* @return SplFileInfo|\PHPUnit_Framework_MockObject_MockObject
* @return SplFileInfo
*/
private function createFile(string $content): SplFileInfo
{
Expand All @@ -141,7 +147,7 @@ private function createFile(string $content): SplFileInfo
->method("getSize")
->willReturn(\strlen($content));

$handle = $this->getMockBuilder(\SplFileObject::class)
$handle = $this->getMockBuilder(SplFileObject::class)
->enableOriginalConstructor()
->setConstructorArgs([tempnam(sys_get_temp_dir(), "")])
->getMock();
Expand All @@ -160,7 +166,7 @@ private function createFile(string $content): SplFileInfo
/**
* @return Parser[][]|FileIteratorInterface[][]
*/
public function unparsableFilesProvider(): array
public function nonParsingFilesProvider(): array
{
$parser = $this->createMock(Parser::class);

Expand Down

0 comments on commit e5e6eeb

Please sign in to comment.