diff --git a/src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php b/src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php index 20d096eb..94522669 100644 --- a/src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php +++ b/src/Inspector/Settings/TargetPhpSettings/TargetPhpSettings.php @@ -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)$'; @@ -23,13 +24,38 @@ final class TargetPhpSettings /** @param value-of $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 $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, + ); } } diff --git a/src/Lib/PhpProcessReader/PhpGlobalsFinder.php b/src/Lib/PhpProcessReader/PhpGlobalsFinder.php index 7b5a177a..9ce1eefe 100644 --- a/src/Lib/PhpProcessReader/PhpGlobalsFinder.php +++ b/src/Lib/PhpProcessReader/PhpGlobalsFinder.php @@ -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 ); diff --git a/tests/Inspector/Settings/TargetPhpSettings/TargetPhpSettingsFromConsoleInputTest.php b/tests/Inspector/Settings/TargetPhpSettings/TargetPhpSettingsFromConsoleInputTest.php index 2f26b3a4..dcac1a26 100644 --- a/tests/Inspector/Settings/TargetPhpSettings/TargetPhpSettingsFromConsoleInputTest.php +++ b/tests/Inspector/Settings/TargetPhpSettings/TargetPhpSettingsFromConsoleInputTest.php @@ -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); diff --git a/tests/Inspector/Settings/TargetPhpSettings/TargetPhpSettingsTest.php b/tests/Inspector/Settings/TargetPhpSettings/TargetPhpSettingsTest.php new file mode 100644 index 00000000..c62914e6 --- /dev/null +++ b/tests/Inspector/Settings/TargetPhpSettings/TargetPhpSettingsTest.php @@ -0,0 +1,49 @@ + + * + * 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()); + } +}