Repository navigation
Reject setSpecification() on Sql\Expression instead of silently ignoring it - #239
Open
simon-mundy wants to merge 1 commit into
Open
simon-mundy wants to merge 1 commit into
simon-mundy wants to merge 1 commit into
Conversation
Expression inherits setSpecification() from AbstractExpression, but getExpressionData() only renders $this->expression, so the override was accepted and silently dropped. Throw a RuntimeException pointing to setExpression() so the caller cannot get different SQL than they asked for. Predicates that honour a custom specification are unchanged.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Sql\ExpressionextendsAbstractExpression, so it inheritssetSpecification(). ButExpression::getExpressionData()builds its spec only from$this->expressionand never reads$this->specification. The call is accepted and has no effect:Predicate\ExpressionextendsSql\Expression, so it behaves the same way.Fix
Expression::setSpecification()is now overridden and throwsSql\Exception\RuntimeException::forUnsupportedSpecificationOverride(). The message names the class and points tosetExpression(). The signature (string $specification): static) is unchanged, so the class still satisfiesAbstractExpression, and subclasses that override it keep working.Why throw rather than alias to
setExpression()setSpecification()on anExpression. That covers docs/book, the test suite,Literal/Predicate\Literal(which don't extendAbstractExpression), TableGateway/RowGateway, and phpdb-mysql, phpdb-sqlite and phpdb-pgsql. The only platform call isSelect::setSpecification()in phpdb-sqlite, which is a different method.sprintfstring whose%sare already resolved. An expression uses?placeholders and has its%escaped. Aliasing one to the other would quietly reinterpret the caller's string, and SQL could still differ from what they wrote. Throwing means the caller can never get SQL other than what they asked for.getSpecification()is unchanged and still returnsnullfor anExpression.BC
Code that called
setSpecification()on anExpressionorPredicate\Expressionnow gets aRuntimeException. That call never changed the rendered SQL, so this only exposes a no-op that was already there.Operator,Like,Between,InandIsNullstill honour a custom specification.Tests
ExpressionTestandPredicate\ExpressionTestcheck thatsetSpecification()throws the new exception and message.Predicate\CustomSpecificationTestuses a data provider to check thatOperator,Like,Between,IsNullandInstill render a custom specification.RuntimeExceptionTestcovers the new named constructor.ExpressionTestclasses now carry#[Group('unit')].docs/book/sql/where-having.mdgains a note under the Expression API.phpunit --testsuite "unit test"passes (2192 tests).mago format --check,mago lintandmago analyzeare clean.Sql\ExpressionandSql\Exception\RuntimeExceptionhave 100% line coverage.