Skip to content

stream_get_contents() does not return FALSE unless an invalid position is provided #4092

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 2 commits into
base: 2.1.x
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,6 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
$stream = $streamOutput->getStream();
rewind($stream);
$baselineContents = stream_get_contents($stream);
if ($baselineContents === false) {
throw new ShouldNotHappenException();
}

try {
DirectoryCreator::ensureDirectoryExists($baselineFileDirectory, 0644);
Expand Down
12 changes: 3 additions & 9 deletions src/Parallel/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use React\Stream\WritableStreamInterface;
use Throwable;
use function fclose;
use function is_string;
use function rewind;
use function sprintf;
use function stream_get_contents;
Expand Down Expand Up @@ -73,16 +72,11 @@ public function start(callable $onData, callable $onError, callable $onExit): vo

$output = '';
rewind($this->stdOut);
$stdOut = stream_get_contents($this->stdOut);
if (is_string($stdOut)) {
$output .= $stdOut;
}
$output .= stream_get_contents($this->stdOut);

rewind($this->stdErr);
$stdErr = stream_get_contents($this->stdErr);
if (is_string($stdErr)) {
$output .= $stdErr;
}
$output .= stream_get_contents($this->stdErr);

$onExit($exitCode, $output);
fclose($this->stdOut);
fclose($this->stdErr);
Expand Down
3 changes: 0 additions & 3 deletions src/Process/ProcessPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public function run(): PromiseInterface
}

if ($exitCode === 0) {
if ($stdOut === false) {
$stdOut = '';
}
$this->deferred->resolve($stdOut);
return;
}
Expand Down
4 changes: 0 additions & 4 deletions src/Testing/ErrorFormatterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ protected function getOutputContent(bool $decorated = false, bool $verbose = fal
rewind($this->getOutputStream($decorated, $verbose)->getStream());

$contents = stream_get_contents($this->getOutputStream($decorated, $verbose)->getStream());
if ($contents === false) {
throw new ShouldNotHappenException();
}

return $this->rtrimMultiline($contents);
}

Expand Down
41 changes: 41 additions & 0 deletions src/Type/Php/StreamGetContentsFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;

#[AutowiredService]
final class StreamGetContentsFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'stream_get_contents';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) >= 3) {
return null;
}

// stream_get_contents() does not return FALSE unless an invalid offset is provided.
$returnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();

return TypeCombinator::remove($returnType, new ConstantBooleanType(false));
}

}
4 changes: 0 additions & 4 deletions tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ private function runPath(string $path, int $expectedStatusCode): string
rewind($output->getStream());

$contents = stream_get_contents($output->getStream());
if ($contents === false) {
throw new ShouldNotHappenException();
}

$this->assertSame($expectedStatusCode, $statusCode, $contents);

return $contents;
Expand Down
6 changes: 0 additions & 6 deletions tests/PHPStan/Command/CommandHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,13 @@ public function testBegin(
if (!$expectException) {
rewind($output->getStream());
$contents = stream_get_contents($output->getStream());
if ($contents === false) {
throw new ShouldNotHappenException();
}
$this->fail($contents);
}
}

rewind($output->getStream());

$contents = stream_get_contents($output->getStream());
if ($contents === false) {
throw new ShouldNotHappenException();
}
$this->assertStringContainsString($expectedOutput, $contents);

if (isset($result)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,6 @@ public function testEndOfFileNewlines(
rewind($outputStream->getStream());

$content = stream_get_contents($outputStream->getStream());
if ($content === false) {
throw new ShouldNotHappenException();
}

if ($expectedNewlinesCount > 0) {
Assert::assertSame(str_repeat("\n", $expectedNewlinesCount), substr($content, -$expectedNewlinesCount));
}
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3572,4 +3572,19 @@ public function testBug13171(): void
]);
}

public function testBug3396(): void
{
$this->checkThisOnly = false;
$this->checkNullables = false;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = false;

$this->analyse([__DIR__ . '/data/bug-3396.php'], [
[
'Parameter #1 $s of method Bug3396\HelloWorld::takesString() expects string, string|false given.',
18,
],
]);
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-3396.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bug3396;

class HelloWorld
{

public function takesString(string $s): void
{
}

public function sayHello(): void
{
$stream = fopen("file.txt", "rb");
if ($stream === false) throw new \Error("wtf");
$this->takesString(stream_get_contents($stream));
$this->takesString(stream_get_contents($stream, 1));
$this->takesString(stream_get_contents($stream, 1, 1));
}
}
Loading