Passed
Push — master ( 1c63d3...be0263 )
by Evgenii
01:34
created

ExampleBlock::queryAssociations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Carbon_Fields\Field;
4
use Helick\Blocks\Block;
5
use WP_Query;
0 ignored issues
show
Bug introduced by
The type WP_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
final class ExampleBlock extends Block
8
{
9
    /**
10
     * The block's display name.
11
     *
12
     * @var string
13
     */
14
    protected $name = 'Example';
15
16
    /**
17
     * The block's description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'This is an example block';
22
23
    /**
24
     * The block's template.
25
     *
26
     * @var string|string[]
27
     */
28
    protected $template = 'partials/blocks/example.php';
29
30
    /**
31
     * Fields to be attached to the block.
32
     *
33
     * @return array
34
     */
35
    public function fields(): array
36
    {
37
        return [
38
            Field::make('text', 'heading', 'Heading'),
39
            Field::make('image', 'image', 'Image'),
40
            Field::make('rich_text', 'content', 'Content'),
41
            Field::make('association', 'associations', 'Associations')
42
                 ->set_types([
0 ignored issues
show
Bug introduced by
The method set_types() does not exist on Carbon_Fields\Field\Field. It seems like you code against a sub-type of Carbon_Fields\Field\Field such as Carbon_Fields\Field\Association_Field. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
                 ->/** @scrutinizer ignore-call */ set_types([
Loading history...
43
                     [
44
                         'type'      => 'post',
45
                         'post_type' => 'post',
46
                     ]
47
                 ])
48
        ];
49
    }
50
51
    /**
52
     * Data to be passed to the rendered block.
53
     *
54
     * @param array $fields
55
     *
56
     * @return array
57
     */
58
    public function with(array $fields): array
59
    {
60
        return [
61
            'associations' => $this->queryAssociations($fields['associations'])
62
        ];
63
    }
64
65
    /**
66
     * Query the associations.
67
     *
68
     * @param array $associations
69
     *
70
     * @return WP_Query
71
     */
72
    private function queryAssociations(array $associations): WP_Query
73
    {
74
        $associationIds = array_column($associations, 'id');
75
        $associationIds = array_map('intval', $associationIds);
76
77
        return new WP_Query([
78
            'no_found_rows' => true,
79
            'post__in'      => $associationIds,
80
            'orderby'       => 'post__in',
81
        ]);
82
    }
83
}
84