Skip to content

Commit f2d6cfe

Browse files
committed
Added ClassRegistry stub
Closes #1
1 parent 3a71297 commit f2d6cfe

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"vendor/cakephp/cakephp/lib/Cake/Controller",
3737
"vendor/cakephp/cakephp/lib/Cake/Event",
3838
"vendor/cakephp/cakephp/lib/Cake/Model",
39+
"vendor/cakephp/cakephp/lib/Cake/Utility",
3940
"vendor/cakephp/cakephp/lib/Cake/View"
4041
]
4142
},

extension.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ parameters:
55
- lib/Cake/Model/Behavior/*.php
66
- app/Plugin/*/Model/Behavior/*.php
77
- app/Model/Behavior/*.php
8+
stubFiles:
9+
- stubs/Utility.php
810
services:
911
- class: ARiddlestone\PHPStanCakePHP2\ControllerComponentsExtension
1012
tags:

stubs/Utility.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
class ClassRegistry
4+
{
5+
/**
6+
* @var array<string,mixed>
7+
*/
8+
protected $_objects = array();
9+
10+
/**
11+
* @var array<string,mixed>
12+
*/
13+
protected $_map = array();
14+
15+
/**
16+
* @var array<string,mixed>
17+
*/
18+
protected $_config = array();
19+
20+
/**
21+
* @return ClassRegistry
22+
*/
23+
public static function getInstance(): ClassRegistry {}
24+
25+
/**
26+
* @template T
27+
* @param class-string<T>|string|array<mixed> $class
28+
* @param bool $strict
29+
* @return ($class is class-string<T> ? T : (bool|object))
30+
*/
31+
public static function init($class, bool $strict = false) {}
32+
33+
/**
34+
* @param string $key
35+
* @param object $object
36+
* @return bool
37+
*/
38+
public static function addObject(string $key, object $object): bool {}
39+
40+
/**
41+
* @param string $key
42+
* @return void
43+
*/
44+
public static function removeObject(string $key): void {}
45+
46+
/**
47+
* @param string $key
48+
* @return bool
49+
*/
50+
public static function isKeySet(string $key): bool {}
51+
52+
/**
53+
* @return array<string>
54+
*/
55+
public static function keys(): array {}
56+
57+
/**
58+
* @param string $key
59+
* @return object|false
60+
*/
61+
public static function getObject(string $key) {}
62+
63+
/**
64+
* @param string $type
65+
* @param array<mixed> $param
66+
* @return mixed
67+
*/
68+
public static function config(string $type, array $param = array()) {}
69+
70+
/**
71+
* @param string $alias
72+
* @param string $class
73+
* @return object|false
74+
*/
75+
protected function &_duplicate(string $alias, string $class) {}
76+
77+
/**
78+
* @param string $key
79+
* @param string $name
80+
* @return void
81+
*/
82+
public static function map(string $key, string $name): void {}
83+
84+
/**
85+
* @return array<string> Keys of registry's map
86+
*/
87+
public static function mapKeys(): array {}
88+
89+
/**
90+
* @param string $key
91+
* @return string
92+
*/
93+
protected function _getMap(string $key): string {}
94+
95+
/**
96+
* @return void
97+
*/
98+
public static function flush(): void {}
99+
}

tests/Stubs/UtilityTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace ARiddlestone\PHPStanCakePHP2\Test\Stubs;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
7+
class UtilityTest extends TypeInferenceTestCase
8+
{
9+
/**
10+
* @return mixed[]
11+
*/
12+
public function dataFileAsserts(): iterable
13+
{
14+
yield from $this->gatherAssertTypes(__DIR__ . '/../data/stubs/class_registry_init.php');
15+
}
16+
17+
/**
18+
* @dataProvider dataFileAsserts
19+
* @param mixed $args
20+
*/
21+
public function testControllerExtensions(string $assertType, string $file, ...$args): void
22+
{
23+
$this->assertFileAsserts($assertType, $file, ...$args);
24+
}
25+
26+
public static function getAdditionalConfigFiles(): array
27+
{
28+
return [
29+
__DIR__ . '/../data/phpstan.neon',
30+
];
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
$class = ClassRegistry::init('BasicModel');
8+
assertType('BasicModel', $class);
9+
10+
$notClass = ClassRegistry::init('NotAClass');
11+
assertType('bool|object', $notClass);

0 commit comments

Comments
 (0)