Skip to content
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"phpstan/phpstan-nette": "^2.0",
"phpstan/phpstan-phpunit": "^2.1",
"phpstan/phpstan-strict-rules": "^2.1",
"phpstan/tally": "dev-release/1.x",
"phpstan/tally": "^1.0.1",
"phpunit/phpunit": "^11.5.23",
"shipmonk/composer-dependency-analyser": "^1.5",
"shipmonk/dead-code-detector": "^1.4.2",
Expand Down
25 changes: 15 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/bench/AnalysedFileExpectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function file(): string
}

#[Override]
public function rewrite(string $code, Metrics $measured): string
public function edits(string $code, Metrics $measured): array
{
return $this->header->write($code, $measured);
return [$this->header->write($code, $measured)];
}

#[Override]
Expand Down
17 changes: 8 additions & 9 deletions tests/bench/AnalysedFileHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace PHPStan\Benchmark;

use Tally\Expectation\CodeEdit;
use Tally\Expectation\ExpectationException;
use Tally\Metric;
use Tally\Metrics;
use Tally\Quantity\InvalidQuantityException;
use function addcslashes;
use function preg_match;
use function preg_replace;
use function sprintf;
use function strlen;
use function substr;

/**
* Reads and writes the expected numbers in the comment after the first line of a benchmark in data/:
Expand All @@ -24,7 +22,7 @@
* * memory: 4.520MiB
* *\/
*
* Takes code and returns code, AnalysedFileExpectations reads the file.
* Takes code and returns the edit of it, AnalysedFileExpectations reads the file.
*/
final class AnalysedFileHeader
{
Expand All @@ -47,25 +45,26 @@ public function read(string $code): ?Metrics
}

/**
* Replaces the comment, or adds it after the <?php line.
*
* @throws ExpectationException
*/
public function write(string $code, Metrics $metrics): string
public function write(string $code, Metrics $metrics): CodeEdit
{
$header = sprintf(
"/*\n * instructions: %s\n * cycles: %s\n * memory: %s\n */\n",
$this->format($metrics, Metric::Instructions),
$this->format($metrics, Metric::Cycles),
$this->format($metrics, Metric::Memory),
);
if (preg_match(self::PATTERN, $code) === 1) {
// the replacement reads $ and \
return (string) preg_replace(self::PATTERN, '$1' . "\n" . addcslashes($header, '$\\'), $code);
if (preg_match(self::PATTERN, $code, $matches) === 1) {
return new CodeEdit(strlen($matches[1]), strlen($matches[0]) - strlen($matches[1]), "\n" . $header);
}
if (preg_match('~\A<\?php[^\n]*\n~', $code, $matches) !== 1) {
throw new ExpectationException('The benchmark does not start with a <?php line.');
}

return $matches[0] . "\n" . $header . substr($code, strlen($matches[0]));
return new CodeEdit(strlen($matches[0]), 0, "\n" . $header);
}

private function format(Metrics $metrics, Metric $metric): string
Expand Down
Loading