@@ -37,36 +37,81 @@ public function processNode(Node $node, Scope $scope): array
37
37
38
38
$ right = $ node ->getArgs ()[1 ]->value ;
39
39
40
- if (
41
- $ right instanceof Node \Expr \FuncCall
42
- && $ right ->name instanceof Node \Name
43
- && $ right ->name ->toLowerString () === 'count '
44
- ) {
45
- return [
46
- RuleErrorBuilder::message ('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)). ' )
47
- ->identifier ('phpunit.assertCount ' )
48
- ->build (),
49
- ];
40
+ $ rightIsCountFuncCall = $ this ->isCountFuncCall ($ right );
41
+ $ rightIsCountMethodCall = $ this ->isCountMethodCall ($ right ) && $ this ->argIsCountable ($ scope , $ right );
42
+ if (!($ rightIsCountFuncCall || $ rightIsCountMethodCall )) {
43
+ return [];
50
44
}
51
45
52
- if (
53
- $ right instanceof Node \Expr \MethodCall
54
- && $ right ->name instanceof Node \Identifier
55
- && $ right ->name ->toLowerString () === 'count '
56
- && count ($ right ->getArgs ()) === 0
57
- ) {
58
- $ type = $ scope ->getType ($ right ->var );
46
+ $ left = $ node ->getArgs ()[0 ]->value ;
47
+ $ leftIsCountFuncCall = $ this ->isCountFuncCall ($ left );
48
+ $ leftIsCountMethodCall = $ this ->isCountMethodCall ($ left ) && $ this ->argIsCountable ($ scope , $ left );
49
+
50
+ if ($ rightIsCountFuncCall ) {
51
+ if ($ leftIsCountFuncCall ) {
52
+ return [
53
+ RuleErrorBuilder::message ('You should use assertSameSize($expected, $variable) instead of assertSame(count($expected), count($variable)). ' )
54
+ ->identifier ('phpunit.assertSameSize ' )
55
+ ->build (),
56
+ ];
57
+ } elseif ($ leftIsCountMethodCall ) {
58
+ return [
59
+ RuleErrorBuilder::message ('You should use assertSameSize($expected, $variable) instead of assertSame($expected->count(), count($variable)). ' )
60
+ ->identifier ('phpunit.assertSameSize ' )
61
+ ->build (),
62
+ ];
59
63
60
- if ((new ObjectType (Countable::class))->isSuperTypeOf ($ type )->yes ()) {
64
+ } else {
65
+ return [
66
+ RuleErrorBuilder::message ('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)). ' )
67
+ ->identifier ('phpunit.assertCount ' )
68
+ ->build (),
69
+ ];
70
+ }
71
+ } else {
72
+ if ($ leftIsCountFuncCall ) {
73
+ return [
74
+ RuleErrorBuilder::message ('You should use assertSameSize($expected, $variable) instead of assertSame(count($expected), $variable->count()). ' )
75
+ ->identifier ('phpunit.assertSameSize ' )
76
+ ->build (),
77
+ ];
78
+ } elseif ($ leftIsCountMethodCall ) {
79
+ return [
80
+ RuleErrorBuilder::message ('You should use assertSameSize($expected, $variable) instead of assertSame($expected->count(), $variable->count()). ' )
81
+ ->identifier ('phpunit.assertSameSize ' )
82
+ ->build (),
83
+ ];
84
+ } else {
61
85
return [
62
86
RuleErrorBuilder::message ('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()). ' )
63
87
->identifier ('phpunit.assertCount ' )
64
88
->build (),
65
89
];
66
90
}
67
91
}
92
+ }
93
+
94
+ private function isCountFuncCall (Node \Expr $ expr ): bool
95
+ {
96
+ return $ expr instanceof Node \Expr \FuncCall
97
+ && $ expr ->name instanceof Node \Name
98
+ && $ expr ->name ->toLowerString () === 'count ' ;
99
+ }
100
+
101
+ private function isCountMethodCall (Node \Expr $ expr ): bool
102
+ {
103
+ return $ expr instanceof Node \Expr \MethodCall
104
+ && $ expr ->name instanceof Node \Identifier
105
+ && $ expr ->name ->toLowerString () === 'count '
106
+ && count ($ expr ->getArgs ()) === 0 ;
107
+ }
108
+
109
+ private function argIsCountable (Scope $ scope , Node \Expr $ expr ): bool
110
+ {
111
+ $ type = $ scope ->getType ($ expr ->var );
112
+ $ countableType = new ObjectType (Countable::class);
68
113
69
- return [] ;
114
+ return $ countableType -> isSuperTypeOf ( $ type )-> yes () ;
70
115
}
71
116
72
117
}
0 commit comments