Completed
Push — master ( d966c0...876bb5 )
by Florian
16:04 queued 58s
created

SectionIOTest::testFlushSiteTree()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 42
rs 8.8571
cc 1
eloc 28
nc 1
nop 0
1
<?php
2
3
class SectionIOTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected static $fixture_file = 'SectionIOTest.yml';
6
7
    public function setUpOnce()
8
    {
9
        parent::setUpOnce();
10
11
        // add config values
12
        Config::inst()->update('SectionIO', 'flush_on_dev_build', true);
13
        Config::inst()->update('SectionIO', 'api_url', 'https://example.com');
14
        Config::inst()->update('SectionIO', 'account_id', '123456');
15
        Config::inst()->update('SectionIO', 'application_id', '987654');
16
        Config::inst()->update('SectionIO', 'environment_name', 'Production');
17
        Config::inst()->update('SectionIO', 'proxy_name', 'myproxy');
18
        Config::inst()->update('SectionIO', 'username', 'someuser');
19
        Config::inst()->update('SectionIO', 'password', 'MySafePassword');
20
21
        // remove extensions otherwise the fixtures will break the tests (by calling the live flush)
22
        File::remove_extension('SectionIOFileExtension');
23
        SiteTree::remove_extension('SectionIOSiteTreeExtension');
24
    }
25
26
    public function setUp()
27
    {
28
        parent::setUp();
29
30
        if (!file_exists(ASSETS_PATH)) {
31
            mkdir(ASSETS_PATH);
32
        }
33
34
        // Create a test folders for each of the fixture references
35
        $folderIDs = $this->allFixtureIDs('Folder');
36 View Code Duplication
        foreach ($folderIDs as $folderID) {
0 ignored issues
show
Bug introduced by
The expression $folderIDs of type object<A> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
37
            $folder = DataObject::get_by_id('Folder', $folderID);
38
            if (!file_exists(BASE_PATH."/$folder->Filename")) {
39
                mkdir(BASE_PATH."/$folder->Filename");
40
            }
41
        }
42
43
        // Copy test images for each of the fixture references
44
        $imageIDs = $this->allFixtureIDs('Image');
45 View Code Duplication
        foreach ($imageIDs as $imageID) {
0 ignored issues
show
Bug introduced by
The expression $imageIDs of type object<A> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
46
            $image = DataObject::get_by_id('Image', $imageID);
47
            $filePath = BASE_PATH."/$image->Filename";
48
            $sourcePath = str_replace('assets/SectionTest/', 'section-io/tests/testfiles/', $filePath);
49
            if (!file_exists($filePath)) {
50
                if (!copy($sourcePath, $filePath)) {
51
                    user_error('Failed to copy test images', E_USER_ERROR);
52
                }
53
            }
54
        }
55
56
        // Copy test files for each of the fixture references
57
        $fileIDs = $this->allFixtureIDs('File');
58 View Code Duplication
        foreach ($fileIDs as $fileID) {
0 ignored issues
show
Bug introduced by
The expression $fileIDs of type object<A> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
59
            $file = DataObject::get_by_id('File', $fileID);
60
            $filePath = BASE_PATH."/$file->Filename";
61
            $sourcePath = str_replace('assets/SectionTest/', 'section-io/tests/testfiles/', $filePath);
62
            if (!file_exists($filePath)) {
63
                if (!copy($sourcePath, $filePath)) {
64
                    user_error('Failed to copy test files', E_USER_ERROR);
65
                }
66
            }
67
        }
68
    }
69
70
    public function tearDownOnce()
71
    {
72
        parent::tearDownOnce();
73
74
        // re-add extensions
75
        File::add_extension('SectionIOFileExtension');
76
        SiteTree::add_extension('SectionIOSiteTreeExtension');
77
    }
78
79
    public function tearDown()
80
    {
81
        // Remove the test images that we've created
82
        $imageIDs = $this->allFixtureIDs('Image');
83 View Code Duplication
        foreach ($imageIDs as $imageID) {
0 ignored issues
show
Bug introduced by
The expression $imageIDs of type object<A> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
84
            $image = DataObject::get_by_id('Image', $imageID);
85
            if ($image && file_exists(BASE_PATH."/$image->Filename")) {
86
                unlink(BASE_PATH."/$image->Filename");
87
            }
88
        }
89
90
        // Remove the test files that we've created
91
        $fileIDs = $this->allFixtureIDs('File');
92 View Code Duplication
        foreach ($fileIDs as $fileID) {
0 ignored issues
show
Bug introduced by
The expression $fileIDs of type object<A> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
93
            $file = DataObject::get_by_id('File', $fileID);
94
            if ($file && file_exists(BASE_PATH."/$file->Filename")) {
95
                unlink(BASE_PATH."/$file->Filename");
96
            }
97
        }
98
99
        // Remove the test folders that we've created
100
        $folderIDs = $this->allFixtureIDs('Folder');
101
        foreach ($folderIDs as $folderID) {
0 ignored issues
show
Bug introduced by
The expression $folderIDs of type object<A> is not traversable.
Loading history...
102
            $folder = DataObject::get_by_id('Folder', $folderID);
103
            if ($folder && file_exists(BASE_PATH.'/'.$folder->Filename.'_resampled')) {
104
                Filesystem::removeFolder(BASE_PATH.'/'.$folder->Filename.'_resampled');
105
            }
106
            if ($folder && file_exists(BASE_PATH."/$folder->Filename")) {
107
                Filesystem::removeFolder(BASE_PATH."/$folder->Filename");
108
            }
109
        }
110
111
        parent::tearDown();
112
    }
113
114
    public function testFlushAll()
115
    {
116
        $result = SectionIOTest_MySectionIO::flushAll();
117
118
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
            1,
120
            $result,
121
            'one url returned for one application id'
122
        );
123
124
        // url
125
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
            'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state',
127
            $result[0]['url'],
128
            'URL is concatenated correctly'
129
        );
130
131
        // ban expression
132
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
            'obj.http.x-url ~ /',
134
            $result[0]['banExpression'],
135
            'ban expression is correct'
136
        );
137
138
        // headers
139
        $this->assertContains(
140
            'Content-Type: application/json',
141
            $result[0]['headers'],
142
            'content type header is correct'
143
        );
144
        $this->assertContains(
145
            'Accept: application/json',
146
            $result[0]['headers'],
147
            'accept header is correct'
148
        );
149
150
        // options
151
        $this->assertArrayHasKey(
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152
            CURLOPT_SSL_VERIFYPEER,
153
            $result[0]['options'],
154
            'ssl verify is set'
155
        );
156
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
            1,
158
            $result[0]['options'][CURLOPT_SSL_VERIFYPEER],
159
            'ssl verfify is activated'
160
        );
161
        $this->assertArrayHasKey(
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
            CURLOPT_SSL_VERIFYHOST,
163
            $result[0]['options'],
164
            'ssl verfi host os set'
165
        );
166
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
            2,
168
            $result[0]['options'][CURLOPT_SSL_VERIFYHOST],
169
            'ssl verfify host is set to 2'
170
        );
171
        $this->assertArrayHasKey(
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
172
            CURLOPT_CAINFO,
173
            $result[0]['options'],
174
            'ca info is set'
175
        );
176
        $this->assertNotEmpty(
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
177
            $result[0]['options'][CURLOPT_CAINFO],
178
            'ca info is not empty'
179
        );
180
181
        // service
182
        $this->assertInstanceOf(
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
183
            'RestfulService',
184
            $result[0]['service'],
185
            'service is of type RestfulService'
186
        );
187
    }
188
189
    public function testFlush()
190
    {
191
        $result = SectionIOTest_MySectionIO::flush();
192
193
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
194
            1,
195
            $result,
196
            'one url returned for one application id'
197
        );
198
199
        // url
200
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201
            'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state',
202
            $result[0]['url'],
203
            'URL is concatenated correctly'
204
        );
205
206
        // ban expression
207
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
208
            'obj.http.x-url ~ /',
209
            $result[0]['banExpression'],
210
            'ban expression is correct'
211
        );
212
213
        // test deactivated flush on build
214
        Config::inst()->update('SectionIO', 'flush_on_dev_build', false);
215
        $result = SectionIOTest_MySectionIO::flush();
216
        $this->assertNull(
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
217
            $result,
218
            'null returned if flush on build deactivated'
219
        );
220
    }
221
222
    public function testMultipleApplicationIDs()
223
    {
224
        // add second application to config
225
        Config::inst()->update('SectionIO', 'application_id', '2546987,856954');
226
227
        $result = SectionIOTest_MySectionIO::flushAll();
228
229
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
230
            2,
231
            $result,
232
            'two urls returned for two application id'
233
        );
234
235
        // url
236
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
237
            'https://example.com/account/123456/application/2546987/environment/Production/proxy/myproxy/state',
238
            $result[0]['url'],
239
            'URL is concatenated correctly for app 1'
240
        );
241
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
242
            'https://example.com/account/123456/application/856954/environment/Production/proxy/myproxy/state',
243
            $result[1]['url'],
244
            'URL is concatenated correctly for app 2'
245
        );
246
247
        // add second application to config with spaces in csv
248
        Config::inst()->update('SectionIO', 'application_id', '741852, 369258');
249
250
        $result = SectionIOTest_MySectionIO::flushAll();
251
252
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
253
            2,
254
            $result,
255
            'two urls returned for two application id'
256
        );
257
258
        // url
259
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
260
            'https://example.com/account/123456/application/741852/environment/Production/proxy/myproxy/state',
261
            $result[0]['url'],
262
            'URL is concatenated correctly for app 1'
263
        );
264
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
265
            'https://example.com/account/123456/application/369258/environment/Production/proxy/myproxy/state',
266
            $result[1]['url'],
267
            'URL is concatenated correctly for app 2'
268
        );
269
    }
270
271
    public function testFlushImage()
272
    {
273
        $imageId = $this->idFromFixture('Image', 'testImage');
274
275
        $result = SectionIOTest_MySectionIO::flushImage($imageId);
276
277
        // ban expression
278
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
279
            'obj.http.x-url ~ "^/assets/SectionTest/test_image\.png$"'
280
                .' || obj.http.x-url ~ "^/assets/SectionTest/_resampled/(.*)\-test_image\.png$"',
281
            $result[0]['banExpression'],
282
            'ban expression is correct'
283
        );
284
    }
285
286
    public function testFlushFile()
287
    {
288
        $fileId = $this->idFromFixture('File', 'testFile');
289
290
        $result = SectionIOTest_MySectionIO::flushFile($fileId);
291
292
        // ban expression
293
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
294
            'obj.http.x-url ~ "^/assets/SectionTest/test_document\.pdf$"',
295
            $result[0]['banExpression'],
296
            'ban expression is correct'
297
        );
298
    }
299
300
    public function testFlushSiteTree()
301
    {
302
        $pageId = $this->idFromFixture('Page', 'ceo');
303
304
        // test single page flush
305
        Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'single');
306
        $result = SectionIOTest_MySectionIO::flushSiteTree($pageId);
307
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
308
            'obj.http.content-type ~ "text/html"'
309
            .' && obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$"',
310
            $result[0]['banExpression'],
311
            'ban expression is correct'
312
        );
313
314
        // test parents flush
315
        Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'parents');
316
        $result = SectionIOTest_MySectionIO::flushSiteTree($pageId);
317
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
318
            'obj.http.content-type ~ "text/html"'
319
            .' && (obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$" || obj.http.x-url ~ "^/about\-us/my\-staff/$" || obj.http.x-url ~ "^/about\-us/$")',
320
            $result[0]['banExpression'],
321
            'ban expression is correct'
322
        );
323
324
        // test all pages flush
325
        Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'all');
326
        $result = SectionIOTest_MySectionIO::flushSiteTree($pageId);
327
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
328
            'obj.http.content-type ~ "text/html"',
329
            $result[0]['banExpression'],
330
            'ban expression is correct'
331
        );
332
333
        // test whole site flush
334
        Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'everything');
335
        $result = SectionIOTest_MySectionIO::flushSiteTree($pageId);
336
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
337
            'obj.http.x-url ~ /',
338
            $result[0]['banExpression'],
339
            'ban expression is correct'
340
        );
341
    }
342
}
343
344
class SectionIOTest_MySectionIO extends SectionIO
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
345
{
346
    protected static function performFlush($banExpression)
347
    {
348
        $result = array();
349
        $urls = static::getUrls();
350
        // config loaded successfully
351
        if ($urls) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $urls of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
352
            foreach ($urls as $url) {
353
354
                // get restful service object
355
                $service = static::getService($url, $banExpression);
356
357
                // prepare headers
358
                $headers = static::getHeaders();
359
360
                // prepare curl options
361
                $options = static::getOptions();
362
363
                // store data for return
364
                $data = array();
365
                $data['url'] = $url;
366
                $data['banExpression'] = $banExpression;
367
                $data['headers'] = $headers;
368
                $data['options'] = $options;
369
                $data['service'] = $service;
370
                $result[] = $data;
371
            }
372
        } else {
373
            user_error('SectionIOTest_MySectionIO::performFlush :: no URLs loaded for ban.', E_USER_WARNING);
374
        }
375
376
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type of the parent method SectionIO::performFlush of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
377
    }
378
}
379