This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | /** |
||
3 | * Copyright (c) 2013 Thomas Tanghus ([email protected]) |
||
4 | * This file is licensed under the Affero General Public License version 3 or |
||
5 | * later. |
||
6 | * See the COPYING-README file. |
||
7 | */ |
||
8 | |||
9 | namespace OCA\Contacts; |
||
10 | |||
11 | use Sabre\VObject\Reader; |
||
12 | use Test\TestCase; |
||
13 | |||
14 | require_once __DIR__ . '/backend/mock.php'; |
||
15 | |||
16 | class AddressBookTest extends TestCase { |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $abinfo; |
||
22 | /** |
||
23 | * @var \OCA\Contacts\Addressbook |
||
24 | */ |
||
25 | protected $ab; |
||
26 | /** |
||
27 | * @var \OCA\Contacts\Backend\AbstractBackend |
||
28 | */ |
||
29 | protected $backend; |
||
30 | |||
31 | public function setUp() { |
||
32 | parent::setUp(); |
||
33 | |||
34 | $this->backend = new Backend\Mock('foobar'); |
||
35 | $this->abinfo = $this->backend->getAddressBook('foo'); |
||
0 ignored issues
–
show
It seems like
$this->backend->getAddressBook('foo') can be null . However, the property $abinfo is declared as array . Maybe change the type of the property to array|null or add a type check?
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property. To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter. function aContainsB(array $needle = null, array $haystack) {
if (!$needle) {
return false;
}
return array_intersect($haystack, $needle) == $haystack;
}
The function can be called with either null or an array for the parameter ![]() |
|||
36 | $this->ab = new AddressBook($this->backend, $this->abinfo); |
||
0 ignored issues
–
show
It seems like
$this->abinfo can also be of type null ; however, OCA\Contacts\Addressbook::__construct() does only seem to accept array , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
37 | |||
38 | } |
||
39 | |||
40 | public function tearDown() { |
||
41 | unset($this->backend); |
||
42 | unset($this->ab); |
||
43 | |||
44 | parent::tearDown(); |
||
45 | } |
||
46 | |||
47 | public function testGetDisplayName() { |
||
48 | |||
49 | $this->assertEquals('d-name', $this->ab->getDisplayName()); |
||
50 | |||
51 | } |
||
52 | |||
53 | public function testGetPermissions() { |
||
54 | |||
55 | $this->assertEquals(\OCP\PERMISSION_ALL, $this->ab->getPermissions()); |
||
56 | |||
57 | } |
||
58 | |||
59 | public function testGetBackend() { |
||
60 | |||
61 | $this->assertEquals($this->backend, $this->ab->getBackend()); |
||
62 | |||
63 | } |
||
64 | |||
65 | public function testGetChild() { |
||
66 | |||
67 | $contact = $this->ab->getChild('123'); |
||
68 | $this->assertInstanceOf('OCA\\Contacts\\Contact', $contact); |
||
69 | $this->assertEquals('Max Mustermann', $contact->getDisplayName()); |
||
70 | |||
71 | } |
||
72 | |||
73 | public function testChildExists() { |
||
74 | |||
75 | $this->assertFalse($this->ab->childExists('12')); |
||
76 | $this->assertTrue($this->ab->childExists('123')); |
||
77 | |||
78 | } |
||
79 | |||
80 | View Code Duplication | public function testAddChild() { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
81 | |||
82 | $carddata = file_get_contents(__DIR__ . '/../data/test2.vcf'); |
||
83 | $vcard = Reader::read($carddata); |
||
84 | $id = $this->ab->addChild($vcard); |
||
0 ignored issues
–
show
It seems like
$vcard defined by \Sabre\VObject\Reader::read($carddata) on line 83 can also be of type object<Sabre\VObject\Component> or object<Sabre\VObject\Property> ; however, OCA\Contacts\Addressbook::addChild() does only seem to accept array|object<OCA\Contacts\VObject\VCard>|null , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
85 | $this->assertNotEquals(false, $id); |
||
86 | |||
87 | return $this->ab; |
||
88 | } |
||
89 | |||
90 | public function testDeleteChild() { |
||
91 | |||
92 | $this->assertTrue($this->ab->deleteChild('123')); |
||
93 | $this->assertEquals(array(), $this->ab->getChildren()); |
||
94 | |||
95 | } |
||
96 | |||
97 | public function testGetChildNotFound() { |
||
98 | |||
99 | try { |
||
100 | $contact = $this->ab->getChild('Nowhere'); |
||
0 ignored issues
–
show
$contact is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
101 | } catch(\Exception $e) { |
||
102 | $this->assertEquals('Contact not found', $e->getMessage()); |
||
103 | $this->assertEquals(404, $e->getCode()); |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | $this->fail('Expected Exception 404.'); |
||
108 | |||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @depends testAddChild |
||
113 | * @param $ab |
||
114 | */ |
||
115 | public function testGetChildren($ab) { |
||
116 | |||
117 | $contacts = $ab->getChildren(); |
||
118 | |||
119 | $this->assertCount(2, $contacts); |
||
120 | |||
121 | $this->assertEquals('Max Mustermann', $contacts[0]->getDisplayName()); |
||
122 | $this->assertEquals('John Q. Public', $contacts[1]->getDisplayName()); |
||
123 | |||
124 | } |
||
125 | |||
126 | public function testDelete() { |
||
127 | |||
128 | $this->assertTrue($this->ab->delete()); |
||
129 | $this->assertEquals(array(), $this->backend->addressBooks); |
||
0 ignored issues
–
show
The property
addressBooks does not seem to exist in OCA\Contacts\Backend\AbstractBackend .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading. ![]() |
|||
130 | |||
131 | } |
||
132 | |||
133 | public function testGetLastModified() { |
||
134 | |||
135 | $this->assertNull($this->ab->lastModified()); |
||
136 | |||
137 | } |
||
138 | |||
139 | View Code Duplication | public function testUpdate() { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
140 | |||
141 | $this->assertTrue( |
||
142 | $this->ab->update(array('displayname' => 'bar')) |
||
143 | ); |
||
144 | |||
145 | $this->assertEquals('bar', $this->backend->addressBooks[0]['displayname']); |
||
0 ignored issues
–
show
The property
addressBooks does not seem to exist in OCA\Contacts\Backend\AbstractBackend .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading. ![]() |
|||
146 | $props = $this->ab->getMetaData(); |
||
0 ignored issues
–
show
$props is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
147 | |||
148 | return $this->ab; |
||
149 | |||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @depends testUpdate |
||
154 | */ |
||
155 | public function testGetMetaData($ab) { |
||
156 | |||
157 | $props = $ab->getMetaData(); |
||
158 | $this->assertEquals('bar', $props['displayname']); |
||
159 | |||
160 | } |
||
161 | |||
162 | public function testArrayAccess() { |
||
163 | |||
164 | $carddata = file_get_contents(__DIR__ . '/../data/test2.vcf'); |
||
165 | $vcard = Reader::read($carddata); |
||
166 | |||
167 | $contact = $this->ab['123']; |
||
168 | |||
169 | // Test get |
||
170 | $this->assertTrue(isset($this->ab['123'])); |
||
171 | $this->assertInstanceOf('OCA\\Contacts\\Contact', $contact); |
||
172 | $this->assertEquals('Max Mustermann', $contact->getDisplayName()); |
||
173 | |||
174 | // Test unset |
||
175 | unset($this->ab['123']); |
||
176 | |||
177 | $this->assertTrue(!isset($this->ab['123'])); |
||
178 | |||
179 | // Test set |
||
180 | try { |
||
181 | $this->ab[] = $vcard; |
||
182 | } catch(\Exception $e) { |
||
183 | return; |
||
184 | } |
||
185 | |||
186 | $this->fail('Expected Exception'); |
||
187 | |||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @depends testAddChild |
||
192 | */ |
||
193 | public function testIterator($ab) { |
||
194 | |||
195 | $count = 0; |
||
196 | |||
197 | foreach($ab as $contact) { |
||
198 | $this->assertInstanceOf('OCA\\Contacts\\Contact', $contact); |
||
199 | $count += 1; |
||
200 | } |
||
201 | |||
202 | $this->assertEquals(2, $count); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @depends testAddChild |
||
207 | */ |
||
208 | public function testCountable($ab) { |
||
209 | |||
210 | $this->assertEquals(2, count($ab)); |
||
211 | |||
212 | } |
||
213 | |||
214 | } |
||
215 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.