Skip to content

Commit ce610b8

Browse files
authored
add types to ConditionalProxy (#113)
* add types to ConditionalProxy * Override conditional query method return type * use better type template name
1 parent 542e3bc commit ce610b8

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

src/Propel/Runtime/ActiveQuery/Criteria.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class Criteria
393393
protected CombineOperatorManager $filterOperatorManager;
394394

395395
/**
396-
* @var \Propel\Runtime\Util\PropelConditionalProxy|null
396+
* @var \Propel\Runtime\Util\PropelConditionalProxy<static>|null
397397
*/
398398
protected $conditionalProxy;
399399

@@ -2273,15 +2273,20 @@ public function _and()
22732273
*
22742274
* @param mixed $cond Casts to bool for variable evaluation
22752275
*
2276-
* @return \Propel\Runtime\ActiveQuery\Criteria|\Propel\Runtime\Util\PropelConditionalProxy
2276+
* @return static Actual return is static|PropelConditionalProxy<static>,
2277+
* but the proxy is transparent (delegates via __call),
2278+
* so we annotate as static for IDE/DX purposes.
22772279
*/
22782280
public function _if($cond)
22792281
{
22802282
$cond = (bool)$cond; // Intentionally not typing the param to allow for evaluation inside this function
22812283

22822284
$this->conditionalProxy = new PropelConditionalProxy($this, $cond, $this->conditionalProxy);
22832285

2284-
return $this->conditionalProxy->getCriteriaOrProxy();
2286+
/** @var static $queryOrProxy */
2287+
$queryOrProxy = $this->conditionalProxy->getCriteriaOrProxy();
2288+
2289+
return $queryOrProxy;
22852290
}
22862291

22872292
/**
@@ -2292,7 +2297,9 @@ public function _if($cond)
22922297
*
22932298
* @throws \Propel\Runtime\Exception\LogicException
22942299
*
2295-
* @return \Propel\Runtime\ActiveQuery\Criteria|\Propel\Runtime\Util\PropelConditionalProxy
2300+
* @return static Actual return is static|PropelConditionalProxy<static>,
2301+
* but the proxy is transparent (delegates via __call),
2302+
* so we annotate as static for IDE/DX purposes.
22962303
*/
22972304
public function _elseif($cond)
22982305
{
@@ -2301,8 +2308,10 @@ public function _elseif($cond)
23012308
if (!$this->conditionalProxy) {
23022309
throw new LogicException(__METHOD__ . ' must be called after _if()');
23032310
}
2311+
/** @var static $queryOrProxy */
2312+
$queryOrProxy = $this->conditionalProxy->_elseif($cond);
23042313

2305-
return $this->conditionalProxy->_elseif($cond);
2314+
return $queryOrProxy;
23062315
}
23072316

23082317
/**
@@ -2311,15 +2320,19 @@ public function _elseif($cond)
23112320
*
23122321
* @throws \Propel\Runtime\Exception\LogicException
23132322
*
2314-
* @return \Propel\Runtime\ActiveQuery\Criteria|\Propel\Runtime\Util\PropelConditionalProxy
2323+
* @return static Actual return is static|PropelConditionalProxy<static>,
2324+
* but the proxy is transparent (delegates via __call),
2325+
* so we annotate as static for IDE/DX purposes.
23152326
*/
23162327
public function _else()
23172328
{
23182329
if (!$this->conditionalProxy) {
23192330
throw new LogicException(__METHOD__ . ' must be called after _if()');
23202331
}
2332+
/** @var static $queryOrProxy */
2333+
$queryOrProxy = $this->conditionalProxy->_else();
23212334

2322-
return $this->conditionalProxy->_else();
2335+
return $queryOrProxy;
23232336
}
23242337

23252338
/**
@@ -2328,7 +2341,9 @@ public function _else()
23282341
*
23292342
* @throws \Propel\Runtime\Exception\LogicException
23302343
*
2331-
* @return \Propel\Runtime\ActiveQuery\Criteria|\Propel\Runtime\Util\PropelConditionalProxy
2344+
* @return static Actual return is static|PropelConditionalProxy<static>,
2345+
* but the proxy is transparent (delegates via __call),
2346+
* so we annotate as static for IDE/DX purposes.
23322347
*/
23332348
public function _endif()
23342349
{
@@ -2337,13 +2352,10 @@ public function _endif()
23372352
}
23382353

23392354
$this->conditionalProxy = $this->conditionalProxy->getParentProxy();
2355+
/** @var static $queryOrNestedProxy */
2356+
$queryOrNestedProxy = $this->conditionalProxy?->getCriteriaOrProxy() ?? $this;
23402357

2341-
if ($this->conditionalProxy) {
2342-
return $this->conditionalProxy->getCriteriaOrProxy();
2343-
}
2344-
2345-
// reached last level
2346-
return $this;
2358+
return $queryOrNestedProxy;
23472359
}
23482360

23492361
/**

src/Propel/Runtime/Util/PropelConditionalProxy.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@
2626
* ->doOtherStuff() // executed
2727
* ->_endif(); // returns $c
2828
* @see Criteria
29+
* @template QueryClass of \Propel\Runtime\ActiveQuery\Criteria
2930
*/
3031
class PropelConditionalProxy
3132
{
3233
/**
33-
* @var \Propel\Runtime\ActiveQuery\Criteria
34+
* @var QueryClass
3435
*/
3536
protected $criteria;
3637

3738
/**
38-
* @var \Propel\Runtime\Util\PropelConditionalProxy|null
39+
* @var \Propel\Runtime\Util\PropelConditionalProxy<QueryClass>|null
3940
*/
4041
protected $parent;
4142

@@ -55,22 +56,18 @@ class PropelConditionalProxy
5556
protected $parentState;
5657

5758
/**
58-
* @param \Propel\Runtime\ActiveQuery\Criteria $criteria
59+
* @param QueryClass $criteria
5960
* @param mixed $cond
60-
* @param self|null $proxy
61+
* @param \Propel\Runtime\Util\PropelConditionalProxy<QueryClass>|null $proxy
6162
*/
62-
public function __construct(Criteria $criteria, $cond, ?self $proxy = null)
63+
public function __construct(Criteria $criteria, $cond, ?PropelConditionalProxy $proxy = null)
6364
{
6465
$this->criteria = $criteria;
6566
$this->wasTrue = false;
6667
$this->setConditionalState($cond);
6768
$this->parent = $proxy;
6869

69-
if ($proxy === null) {
70-
$this->parentState = true;
71-
} else {
72-
$this->parentState = $proxy->getConditionalState();
73-
}
70+
$this->parentState = $proxy?->getConditionalState() ?? true;
7471
}
7572

7673
/**
@@ -79,12 +76,10 @@ public function __construct(Criteria $criteria, $cond, ?self $proxy = null)
7976
*
8077
* @param mixed $cond Casts to bool for variable evaluation
8178
*
82-
* @return \Propel\Runtime\ActiveQuery\Criteria|\Propel\Runtime\Util\PropelConditionalProxy
79+
* @return QueryClass|\Propel\Runtime\Util\PropelConditionalProxy<QueryClass>
8380
*/
8481
public function _if($cond)
8582
{
86-
$cond = (bool)$cond; // Intentionally not typing the param to allow for evaluation inside this function
87-
8883
return $this->criteria->_if($cond);
8984
}
9085

@@ -93,7 +88,7 @@ public function _if($cond)
9388
*
9489
* @param mixed $cond Casts to bool for variable evaluation
9590
*
96-
* @return $this|\Propel\Runtime\ActiveQuery\Criteria
91+
* @return $this|QueryClass
9792
*/
9893
public function _elseif($cond)
9994
{
@@ -105,7 +100,7 @@ public function _elseif($cond)
105100
/**
106101
* Allows for conditional statements in a fluid interface.
107102
*
108-
* @return $this|\Propel\Runtime\ActiveQuery\Criteria
103+
* @return $this|QueryClass
109104
*/
110105
public function _else()
111106
{
@@ -116,7 +111,7 @@ public function _else()
116111
* Returns the parent object
117112
* Allows for conditional statements in a fluid interface.
118113
*
119-
* @return \Propel\Runtime\ActiveQuery\Criteria|\Propel\Runtime\Util\PropelConditionalProxy
114+
* @return QueryClass|\Propel\Runtime\Util\PropelConditionalProxy<QueryClass>
120115
*/
121116
public function _endif()
122117
{
@@ -136,7 +131,7 @@ protected function getConditionalState(): bool
136131
/**
137132
* @param mixed $cond
138133
*
139-
* @return $this|\Propel\Runtime\ActiveQuery\Criteria
134+
* @return $this|QueryClass
140135
*/
141136
protected function setConditionalState($cond)
142137
{
@@ -147,23 +142,21 @@ protected function setConditionalState($cond)
147142
}
148143

149144
/**
150-
* @return self|null
145+
* @return self<QueryClass>|null
151146
*/
152147
public function getParentProxy(): ?self
153148
{
154149
return $this->parent;
155150
}
156151

157152
/**
158-
* @return $this|\Propel\Runtime\ActiveQuery\Criteria
153+
* @return $this|QueryClass
159154
*/
160155
public function getCriteriaOrProxy()
161156
{
162-
if ($this->state && $this->parentState) {
163-
return $this->criteria;
164-
}
165-
166-
return $this;
157+
return $this->state && $this->parentState
158+
? $this->criteria
159+
: $this;
167160
}
168161

169162
/**

0 commit comments

Comments
 (0)