Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07a010f

Browse files
committed
Fix MissingPropertyFromReflectionException when working with Stripe SDK
1 parent 1de1dad commit 07a010f

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

src/Type/ObjectShapeType.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,20 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
173173
}
174174

175175
$result = $result->and($hasProperty);
176-
$otherProperty = $type->getProperty($propertyName, $scope);
176+
try {
177+
$otherProperty = $type->getProperty($propertyName, $scope);
178+
} catch (MissingPropertyFromReflectionException) {
179+
return AcceptsResult::createNo(
180+
[
181+
sprintf(
182+
'%s does not have property $%s.',
183+
$type->describe(VerbosityLevel::typeOnly()),
184+
$propertyName,
185+
),
186+
],
187+
);
188+
}
189+
177190
if (!$otherProperty->isPublic()) {
178191
return new AcceptsResult(TrinaryLogic::createNo(), [
179192
sprintf('Property %s::$%s is not public.', $otherProperty->getDeclaringClass()->getDisplayName(), $propertyName),
@@ -265,7 +278,19 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
265278
}
266279

267280
$result = $result->and($hasProperty);
268-
$otherProperty = $type->getProperty($propertyName, $scope);
281+
try {
282+
$otherProperty = $type->getProperty($propertyName, $scope);
283+
} catch (MissingPropertyFromReflectionException) {
284+
return IsSuperTypeOfResult::createNo(
285+
[
286+
sprintf(
287+
'%s does not have property $%s.',
288+
$type->describe(VerbosityLevel::typeOnly()),
289+
$propertyName,
290+
),
291+
],
292+
);
293+
}
269294
if (!$otherProperty->isPublic()) {
270295
return IsSuperTypeOfResult::createNo();
271296
}

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,12 @@ public function testDiscussion9053(): void
12521252
$this->assertNoErrors($errors);
12531253
}
12541254

1255+
public function testBug13492(): void
1256+
{
1257+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-13492.php');
1258+
$this->assertNoErrors($errors);
1259+
}
1260+
12551261
public function testProcessCalledMethodInfiniteLoop(): void
12561262
{
12571263
$errors = $this->runAnalyse(__DIR__ . '/data/process-called-method-infinite-loop.php');
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Bug13492;
4+
5+
/**
6+
* @property CustomerService $customers
7+
*/
8+
class StripeClient
9+
{
10+
11+
/**
12+
* @return mixed
13+
*/
14+
public function __get(string $name)
15+
{
16+
return $this->getService();
17+
}
18+
19+
public function getService(): CustomerService
20+
{
21+
return new CustomerService();
22+
}
23+
24+
}
25+
26+
class CustomerService
27+
{
28+
29+
public function create(): Customer
30+
{
31+
return new Customer();
32+
}
33+
34+
}
35+
36+
/**
37+
* @property null|(object{address?: (object{city: null|string, country: null|string, line1: null|string, line2: null|string, postal_code: null|string, state: null|string}&StripeObject), carrier?: null|string, name?: string, phone?: null|string, tracking_number?: null|string}&StripeObject) $shipping Mailing and shipping address for the customer. Appears on invoices emailed to this customer.
38+
* @property null|(object{city: null|string, country: null|string, line1: null|string, line2: null|string, postal_code: null|string, state: null|string}&StripeObject) $address The customer's address.
39+
*/
40+
class Customer extends StripeObject
41+
{
42+
43+
}
44+
45+
class StripeObject
46+
{
47+
/** @return mixed */
48+
public function &__get(string $k)
49+
{
50+
51+
}
52+
}
53+
54+
function (): void {
55+
$stripe = new StripeClient();
56+
$customer = $stripe->customers->create();
57+
};

0 commit comments

Comments
 (0)