Skip to content

Commit

Permalink
make TargetPhpSettings immutable and add a way to alter its target ph…
Browse files Browse the repository at this point in the history
…p version
  • Loading branch information
sj-i committed Dec 25, 2021
1 parent f05ad5b commit f185571
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
34 changes: 30 additions & 4 deletions src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use PhpProfiler\Lib\PhpInternals\ZendTypeReader;

/** @psalm-immutable */
final class TargetPhpSettings
{
public const PHP_REGEX_DEFAULT = '.*/(php(74|7.4|80|8.0)?|php-fpm|libphp[78]?.*\.so)$';
Expand All @@ -23,13 +24,38 @@ final class TargetPhpSettings

/** @param value-of<ZendTypeReader::ALL_SUPPORTED_VERSIONS> $php_version */
public function __construct(
public string $php_regex = self::PHP_REGEX_DEFAULT,
public string $libpthread_regex = self::LIBPTHREAD_REGEX_DEFAULT,
private string $php_regex = self::PHP_REGEX_DEFAULT,
private string $libpthread_regex = self::LIBPTHREAD_REGEX_DEFAULT,
public string $php_version = self::TARGET_PHP_VERSION_DEFAULT,
public ?string $php_path = null,
public ?string $libpthread_path = null
) {
$this->php_regex = '{' . $php_regex . '}';
$this->libpthread_regex = '{' . $libpthread_regex . '}';
}

public function getDelimitedPhpRegex(): string
{
return $this->getDelimitedRegex($this->php_regex);
}

public function getDelimitedLibPthreadRegex(): string
{
return $this->getDelimitedRegex($this->libpthread_regex);
}

private function getDelimitedRegex(string $regex): string
{
return '{' . $regex . '}';
}

/** @param value-of<ZendTypeReader::ALL_SUPPORTED_VERSIONS> $php_version */
public function alterPhpVersion(string $php_version): self
{
return new self(
php_regex: $this->php_regex,
libpthread_regex: $this->libpthread_regex,
php_version: $php_version,
php_path: $this->php_path,
libpthread_path: $this->libpthread_path,
);
}
}
4 changes: 2 additions & 2 deletions src/Lib/PhpProcessReader/PhpGlobalsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function getSymbolReader(
): ProcessSymbolReaderInterface {
return $this->php_symbol_reader_creator->create(
$process_specifier->pid,
$target_php_settings->php_regex,
$target_php_settings->libpthread_regex,
$target_php_settings->getDelimitedPhpRegex(),
$target_php_settings->getDelimitedLibPthreadRegex(),
$target_php_settings->php_path,
$target_php_settings->libpthread_path
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function testFromConsoleInput(): void

$settings = (new TargetPhpSettingsFromConsoleInput())->createSettings($input);

$this->assertSame('{abc}', $settings->php_regex);
$this->assertSame('{def}', $settings->libpthread_regex);
$this->assertSame('{abc}', $settings->getDelimitedPhpRegex());
$this->assertSame('{def}', $settings->getDelimitedLibPthreadRegex());
$this->assertSame('v74', $settings->php_version);
$this->assertSame('ghi', $settings->php_path);
$this->assertSame('jkl', $settings->libpthread_path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* This file is part of the sj-i/php-profiler package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Inspector\Settings\TargetPhpSettings;

use PhpProfiler\Lib\PhpInternals\ZendTypeReader;
use PHPUnit\Framework\TestCase;

class TargetPhpSettingsTest extends TestCase
{
public function testAlterPhpVersion(): void
{
$settings = new TargetPhpSettings(
php_version: ZendTypeReader::V74,
);
$settings_altered = $settings->alterPhpVersion(ZendTypeReader::V81);
$this->assertSame($settings->php_path, $settings_altered->php_path);
$this->assertSame($settings->getDelimitedPhpRegex(), $settings_altered->getDelimitedPhpRegex());
$this->assertSame($settings->libpthread_path, $settings_altered->libpthread_path);
$this->assertSame($settings->getDelimitedLibPthreadRegex(), $settings_altered->getDelimitedLibPthreadRegex());
$this->assertSame(ZendTypeReader::V81, $settings_altered->php_version);
}

public function testGetDelimitedPhpRegex(): void
{
$settings = new TargetPhpSettings(
php_regex: 'test',
);
$this->assertSame('{test}', $settings->getDelimitedPhpRegex());
}

public function testGetDelimitedLibPthreadRegex(): void
{
$settings = new TargetPhpSettings(
libpthread_regex: 'test',
);
$this->assertSame('{test}', $settings->getDelimitedLibPthreadRegex());
}
}

0 comments on commit f185571

Please sign in to comment.