@@ -451,16 +451,26 @@ public function validateClauseOrder($parser, $list)
451451
452452 /**
453453 * For tracking JOIN clauses in a query
454- * 0 - JOIN not found till now
455- * 1 - JOIN has been found
456- * 2 - A Non-JOIN clause has been found
457- * after a previously found JOIN clause.
454+ * = 0 - JOIN not found till now
455+ * > 0 - Index of first JOIN clause in the statement
458456 *
459457 * @var int
460458 */
461- $ joinStart = 0 ;
459+ $ minJoin = 0 ;
460+
461+ /**
462+ * For tracking JOIN clauses in a query
463+ * = 0 - JOIN not found till now
464+ * > 0 - Index of last JOIN clause
465+ * (which appears together with other JOINs)
466+ * in the statement
467+ *
468+ * @var int
469+ */
470+ $ maxJoin = 0 ;
462471
463472 $ error = 0 ;
473+ $ lastIdx = 0 ;
464474 foreach ($ clauses as $ clauseType => $ index ) {
465475 $ clauseStartIdx = Utils \Query::getClauseStartOffset (
466476 $ this ,
@@ -470,17 +480,19 @@ public function validateClauseOrder($parser, $list)
470480
471481 // Handle ordering of Multiple Joins in a query
472482 if ($ clauseStartIdx != -1 ) {
473- if ($ joinStart == 0 && stripos ($ clauseType , 'JOIN ' ) !== false ) {
474- $ joinStart = 1 ;
475- } elseif ($ joinStart == 1 && stripos ($ clauseType , 'JOIN ' ) === false ) {
476- $ joinStart = 2 ;
477- } elseif ($ joinStart == 2 && stripos ($ clauseType , 'JOIN ' ) !== false ) {
483+ if ($ minJoin === 0 && stripos ($ clauseType , 'JOIN ' )) {
484+ // First JOIN clause is detected
485+ $ minJoin = $ maxJoin = $ clauseStartIdx ;
486+ } elseif ($ minJoin !== 0 && ! stripos ($ clauseType , 'JOIN ' )) {
487+ // After a previous JOIN clause, a non-JOIN clause has been detected
488+ $ maxJoin = $ lastIdx ;
489+ } elseif ($ maxJoin < $ clauseStartIdx && stripos ($ clauseType , 'JOIN ' )) {
478490 $ error = 1 ;
479491 }
480492 }
481493
482494 if ($ clauseStartIdx != -1 && $ clauseStartIdx < $ minIdx ) {
483- if ($ joinStart == 0 || ( $ joinStart == 2 && $ error = 1 ) ) {
495+ if ($ minJoin === 0 || $ error === 1 ) {
484496 $ token = $ list ->tokens [$ clauseStartIdx ];
485497 $ parser ->error (
486498 'Unexpected ordering of clauses. ' ,
@@ -493,6 +505,8 @@ public function validateClauseOrder($parser, $list)
493505 } elseif ($ clauseStartIdx != -1 ) {
494506 $ minIdx = $ clauseStartIdx ;
495507 }
508+
509+ $ lastIdx = ($ clauseStartIdx !== -1 ) ? $ clauseStartIdx : $ lastIdx ;
496510 }
497511
498512 return true ;
0 commit comments