diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-08 03:04:27 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-08 03:04:27 +0900 |
| commit | da0d38a8e16ebefd59ef5291b5788e6238cc78ba (patch) | |
| tree | c6a4cb9f2444beeb84b2d30133053867acb46741 /crates/shirabe/src/plugin | |
| parent | c0879ab4a02b257cb0bd416dbdfa07784c8e3809 (diff) | |
| download | php-shirabe-da0d38a8e16ebefd59ef5291b5788e6238cc78ba.tar.gz php-shirabe-da0d38a8e16ebefd59ef5291b5788e6238cc78ba.tar.zst php-shirabe-da0d38a8e16ebefd59ef5291b5788e6238cc78ba.zip | |
refactor(semver): replace Constraint's OP_*/STR_OP_* with CmpOp
PHP's version_compare takes its operator as a string, so the shim's port
did too, and Constraint carried two families of operator constants plus
translation tables to convert between the string form and its own int
codes. Five copies of those tables had accumulated across Constraint,
CompilingMatcher and the plugin value bridge.
version_compare now takes a CmpOp, which makes an invalid operator
unrepresentable and removes the tables' reason to exist. Constraint
stores a CmpOp and keeps only the string parsing its constructor needs;
getOperator, compile and CompilingMatcher::match speak CmpOp as well.
PHP's OP_* numbering stays observable: a plugin reads the raw integer off
the Constraint object over RPC, so get_operator_constant and its new
inverse hold that 0..5 mapping.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/plugin')
| -rw-r--r-- | crates/shirabe/src/plugin/php_plugin_value.rs | 25 | ||||
| -rw-r--r-- | crates/shirabe/src/plugin/plugin_manager.rs | 6 |
2 files changed, 9 insertions, 22 deletions
diff --git a/crates/shirabe/src/plugin/php_plugin_value.rs b/crates/shirabe/src/plugin/php_plugin_value.rs index cdef2a79..a61d4873 100644 --- a/crates/shirabe/src/plugin/php_plugin_value.rs +++ b/crates/shirabe/src/plugin/php_plugin_value.rs @@ -68,20 +68,6 @@ fn required_string(context: &str, value: Option<&PluginValue>) -> Result<String, } } -/// PHP's `Constraint` keeps the operator as one of its `OP_*` codes, not as the string its -/// constructor takes. -fn operator_from_code(code: i64) -> Option<&'static str> { - Some(match code { - SimpleConstraint::OP_EQ => SimpleConstraint::STR_OP_EQ, - SimpleConstraint::OP_LT => SimpleConstraint::STR_OP_LT, - SimpleConstraint::OP_LE => SimpleConstraint::STR_OP_LE, - SimpleConstraint::OP_GT => SimpleConstraint::STR_OP_GT, - SimpleConstraint::OP_GE => SimpleConstraint::STR_OP_GE, - SimpleConstraint::OP_NE => SimpleConstraint::STR_OP_NE, - _ => return None, - }) -} - fn constraint_to_wire(constraint: &AnyConstraint) -> PluginValue { // Whether the pretty string was ever set is observable (`getPrettyString()` falls back to the // string form), so an unset one crosses as null rather than as an absent property. @@ -144,11 +130,12 @@ fn constraint_from_wire(value: &PluginValue) -> Result<AnyConstraint, PhpThrow> Ok(match object.class.as_str() { CONSTRAINT_CLASS => { let operator = match object.protected("operator") { - Some(PluginValue::Int(code)) => operator_from_code(*code).ok_or_else(|| { - throw(format!( - "a semver constraint has an unknown operator: {code}" - )) - })?, + Some(PluginValue::Int(code)) => SimpleConstraint::from_operator_constant(*code) + .ok_or_else(|| { + throw(format!( + "a semver constraint has an unknown operator: {code}" + )) + })?, other => { return Err(throw(format!( "a semver constraint operator is not an int, got {other:?}" diff --git a/crates/shirabe/src/plugin/plugin_manager.rs b/crates/shirabe/src/plugin/plugin_manager.rs index 8f005079..18239ce9 100644 --- a/crates/shirabe/src/plugin/plugin_manager.rs +++ b/crates/shirabe/src/plugin/plugin_manager.rs @@ -27,7 +27,7 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_rpc::{PluginValue, call_function_with_dispatcher}; use shirabe_php_shim::{ - E_USER_DEPRECATED, PhpMixed, RuntimeException, UnexpectedValueException, dirname, empty, + CmpOp, E_USER_DEPRECATED, PhpMixed, RuntimeException, UnexpectedValueException, dirname, empty, file_get_contents, implode, ksort, php_regex, preg_quote, strrpos, strtr_array, substr, trigger_error, trim, var_export, var_export_str, version_compare, }; @@ -263,7 +263,7 @@ impl PluginManager { if package.get_name() == "symfony/flex" && Preg::is_match3(php_regex!("{^[0-9.]+$}"), &package.get_version(), None) - && version_compare(&package.get_version(), "1.9.8", "<") + && version_compare(&package.get_version(), "1.9.8", CmpOp::Lt) { self.io.write_error(&format!("<warning>The \"{}\" plugin {}was skipped because it is not compatible with Composer 2+. Make sure to update it to version 1.9.8 or greater.</warning>", package.get_name(), @@ -1171,7 +1171,7 @@ impl PluginManager { Some(l) => { if l.is_locked() { let api = l.get_plugin_api().unwrap_or_default(); - version_compare(&api, "2.2.0", "<") + version_compare(&api, "2.2.0", CmpOp::Lt) } else { false } |
