Passed
Push — master ( 94abca...7bb122 )
by Greg
05:28
created

ModuleDataFixTrait::recordsToFix()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 8
eloc 26
nc 128
nop 2
dl 0
loc 44
rs 8.2111
c 1
b 1
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Family;
23
use Fisharebest\Webtrees\GedcomRecord;
24
use Fisharebest\Webtrees\Individual;
25
use Fisharebest\Webtrees\Media;
26
use Fisharebest\Webtrees\Note;
27
use Fisharebest\Webtrees\Repository;
28
use Fisharebest\Webtrees\Source;
29
use Fisharebest\Webtrees\Submitter;
30
use Fisharebest\Webtrees\Tree;
31
use Illuminate\Database\Capsule\Manager as DB;
32
use Illuminate\Database\Query\Builder;
33
use Illuminate\Support\Collection;
34
use stdClass;
35
36
/**
37
 * Trait ModuleDataFixTrait - default implementation of ModuleDataFixTrait
38
 */
39
trait ModuleDataFixTrait
40
{
41
    /**
42
     * Options form.
43
     *
44
     * @param Tree $tree
45
     *
46
     * @return string
47
     */
48
    public function fixOptions(Tree $tree): string
0 ignored issues
show
Unused Code introduced by
The parameter $tree is not used and could be removed. ( Ignorable by Annotation )

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

48
    public function fixOptions(/** @scrutinizer ignore-unused */ Tree $tree): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        return '';
51
    }
52
53
    /**
54
     * A list of all records that need examining.  This may include records
55
     * that do not need updating, if we can't detect this quickly using SQL.
56
     *
57
     * @param Tree                 $tree
58
     * @param array<string,string> $params
59
     *
60
     * @return Collection<stdClass>
61
     */
62
    public function recordsToFix(Tree $tree, array $params): Collection
63
    {
64
        $families     = $this->familiesToFix($tree, $params);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $families is correct as $this->familiesToFix($tree, $params) targeting Fisharebest\Webtrees\Mod...xTrait::familiesToFix() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
        $individuals  = $this->individualsToFix($tree, $params);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $individuals is correct as $this->individualsToFix($tree, $params) targeting Fisharebest\Webtrees\Mod...ait::individualsToFix() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
        $media        = $this->mediaToFix($tree, $params);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $media is correct as $this->mediaToFix($tree, $params) targeting Fisharebest\Webtrees\Mod...aFixTrait::mediaToFix() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
        $notes        = $this->notesToFix($tree, $params);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $notes is correct as $this->notesToFix($tree, $params) targeting Fisharebest\Webtrees\Mod...aFixTrait::notesToFix() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
        $repositories = $this->repositoriesToFix($tree, $params);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $repositories is correct as $this->repositoriesToFix($tree, $params) targeting Fisharebest\Webtrees\Mod...it::repositoriesToFix() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
69
        $sources      = $this->sourcesToFix($tree, $params);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $sources is correct as $this->sourcesToFix($tree, $params) targeting Fisharebest\Webtrees\Mod...ixTrait::sourcesToFix() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
        $submitters   = $this->submittersToFix($tree, $params);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $submitters is correct as $this->submittersToFix($tree, $params) targeting Fisharebest\Webtrees\Mod...rait::submittersToFix() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
72
        $records = new Collection();
73
74
        if ($families !== null) {
1 ignored issue
show
introduced by
The condition $families !== null is always false.
Loading history...
75
            $records = $records->concat($this->mergePendingRecords($families, $tree, Family::RECORD_TYPE));
76
        }
77
78
        if ($individuals !== null) {
1 ignored issue
show
introduced by
The condition $individuals !== null is always false.
Loading history...
79
            $records = $records->concat($this->mergePendingRecords($individuals, $tree, Individual::RECORD_TYPE));
80
        }
81
82
        if ($media !== null) {
1 ignored issue
show
introduced by
The condition $media !== null is always false.
Loading history...
83
            $records = $records->concat($this->mergePendingRecords($media, $tree, Media::RECORD_TYPE));
84
        }
85
86
        if ($notes !== null) {
1 ignored issue
show
introduced by
The condition $notes !== null is always false.
Loading history...
87
            $records = $records->concat($this->mergePendingRecords($notes, $tree, Note::RECORD_TYPE));
88
        }
89
90
        if ($repositories !== null) {
1 ignored issue
show
introduced by
The condition $repositories !== null is always false.
Loading history...
91
            $records = $records->concat($this->mergePendingRecords($repositories, $tree, Repository::RECORD_TYPE));
92
        }
93
94
        if ($sources !== null) {
1 ignored issue
show
introduced by
The condition $sources !== null is always false.
Loading history...
95
            $records = $records->concat($this->mergePendingRecords($sources, $tree, Source::RECORD_TYPE));
96
        }
97
98
        if ($submitters !== null) {
1 ignored issue
show
introduced by
The condition $submitters !== null is always false.
Loading history...
99
            $records = $records->concat($this->mergePendingRecords($submitters, $tree, Submitter::RECORD_TYPE));
100
        }
101
102
        return $records
103
            ->unique()
104
            ->sort(static function (stdClass $x, stdClass $y) {
105
                return $x->xref <=> $y->xref;
106
            });
107
    }
108
109
    /**
110
     * Does a record need updating?
111
     *
112
     * @param GedcomRecord         $record
113
     * @param array<string,string> $params
114
     *
115
     * @return bool
116
     */
117
    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

117
    public function doesRecordNeedUpdate(GedcomRecord $record, /** @scrutinizer ignore-unused */ array $params): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $record is not used and could be removed. ( Ignorable by Annotation )

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

117
    public function doesRecordNeedUpdate(/** @scrutinizer ignore-unused */ GedcomRecord $record, array $params): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119
        return false;
120
    }
121
122
    /**
123
     * Show the changes we would make
124
     *
125
     * @param GedcomRecord         $record
126
     * @param array<string,string> $params
127
     *
128
     * @return string
129
     */
130
    public function previewUpdate(GedcomRecord $record, array $params): string
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

130
    public function previewUpdate(GedcomRecord $record, /** @scrutinizer ignore-unused */ array $params): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        return $record->fullName();
133
    }
134
135
    /**
136
     * Fix a record
137
     *
138
     * @param GedcomRecord         $record
139
     * @param array<string,string> $params
140
     *
141
     * @return void
142
     */
143
    public function updateRecord(GedcomRecord $record, array $params): void
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

143
    public function updateRecord(GedcomRecord $record, /** @scrutinizer ignore-unused */ array $params): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $record is not used and could be removed. ( Ignorable by Annotation )

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

143
    public function updateRecord(/** @scrutinizer ignore-unused */ GedcomRecord $record, array $params): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
    {
145
    }
146
147
    /**
148
     * XREFs of family records that might need fixing.
149
     *
150
     * @param Tree                 $tree
151
     * @param array<string,string> $params
152
     *
153
     * @return Collection<string>|null
154
     */
155
    protected function familiesToFix(Tree $tree, array $params): ?Collection
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

155
    protected function familiesToFix(Tree $tree, /** @scrutinizer ignore-unused */ array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tree is not used and could be removed. ( Ignorable by Annotation )

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

155
    protected function familiesToFix(/** @scrutinizer ignore-unused */ Tree $tree, array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157
        return null;
158
    }
159
160
    /**
161
     * XREFs of individual records that might need fixing.
162
     *
163
     * @param Tree                 $tree
164
     * @param array<string,string> $params
165
     *
166
     * @return Collection<string>|null
167
     */
168
    protected function individualsToFix(Tree $tree, array $params): ?Collection
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

168
    protected function individualsToFix(Tree $tree, /** @scrutinizer ignore-unused */ array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tree is not used and could be removed. ( Ignorable by Annotation )

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

168
    protected function individualsToFix(/** @scrutinizer ignore-unused */ Tree $tree, array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
    {
170
        return null;
171
    }
172
173
    /**
174
     * XREFs of media records that might need fixing.
175
     *
176
     * @param Tree                 $tree
177
     * @param array<string,string> $params
178
     *
179
     * @return Collection<string>|null
180
     */
181
    protected function mediaToFix(Tree $tree, array $params): ?Collection
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

181
    protected function mediaToFix(Tree $tree, /** @scrutinizer ignore-unused */ array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tree is not used and could be removed. ( Ignorable by Annotation )

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

181
    protected function mediaToFix(/** @scrutinizer ignore-unused */ Tree $tree, array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
    {
183
        return null;
184
    }
185
186
    /**
187
     * XREFs of note records that might need fixing.
188
     *
189
     * @param Tree                 $tree
190
     * @param array<string,string> $params
191
     *
192
     * @return Collection<string>|null
193
     */
194
    protected function notesToFix(Tree $tree, array $params): ?Collection
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

194
    protected function notesToFix(Tree $tree, /** @scrutinizer ignore-unused */ array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tree is not used and could be removed. ( Ignorable by Annotation )

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

194
    protected function notesToFix(/** @scrutinizer ignore-unused */ Tree $tree, array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
    {
196
        return null;
197
    }
198
199
    /**
200
     * XREFs of repository records that might need fixing.
201
     *
202
     * @param Tree                 $tree
203
     * @param array<string,string> $params
204
     *
205
     * @return Collection<string>|null
206
     */
207
    protected function repositoriesToFix(Tree $tree, array $params): ?Collection
0 ignored issues
show
Unused Code introduced by
The parameter $tree is not used and could be removed. ( Ignorable by Annotation )

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

207
    protected function repositoriesToFix(/** @scrutinizer ignore-unused */ Tree $tree, array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

207
    protected function repositoriesToFix(Tree $tree, /** @scrutinizer ignore-unused */ array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
208
    {
209
        return null;
210
    }
211
212
    /**
213
     * XREFs of source records that might need fixing.
214
     *
215
     * @param Tree                 $tree
216
     * @param array<string,string> $params
217
     *
218
     * @return Collection<string>|null
219
     */
220
    protected function sourcesToFix(Tree $tree, array $params): ?Collection
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

220
    protected function sourcesToFix(Tree $tree, /** @scrutinizer ignore-unused */ array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tree is not used and could be removed. ( Ignorable by Annotation )

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

220
    protected function sourcesToFix(/** @scrutinizer ignore-unused */ Tree $tree, array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
221
    {
222
        return null;
223
    }
224
225
    /**
226
     * XREFs of submitter records that might need fixing.
227
     *
228
     * @param Tree                 $tree
229
     * @param array<string,string> $params
230
     *
231
     * @return Collection<string>|null
232
     */
233
    protected function submittersToFix(Tree $tree, array $params): ?Collection
0 ignored issues
show
Unused Code introduced by
The parameter $tree is not used and could be removed. ( Ignorable by Annotation )

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

233
    protected function submittersToFix(/** @scrutinizer ignore-unused */ Tree $tree, array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

233
    protected function submittersToFix(Tree $tree, /** @scrutinizer ignore-unused */ array $params): ?Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
234
    {
235
        return null;
236
    }
237
238
    /**
239
     * Merge pending changes of a given type.  We need to check all pending records.
240
     *
241
     * @param Collection<string> $records
242
     * @param Tree               $tree
243
     * @param string             $type
244
     *
245
     * @return Collection<stdClass>
246
     */
247
    private function mergePendingRecords(Collection $records, Tree $tree, string $type): Collection
248
    {
249
        $pending = DB::table('change')
250
            ->where('gedcom_id', '=', $tree->id())
251
            ->where('status', '=', 'pending')
252
            ->where(static function (Builder $query) use ($type): void {
253
                $query
254
                    ->where('old_gedcom', 'LIKE', '%@ ' . $type . '%')
255
                    ->orWhere('new_gedcom', 'LIKE', '%@ ' . $type . '%');
256
            })
257
            ->pluck('xref');
258
259
        return $records
260
            ->concat($pending)
261
            ->map(static function (string $xref) use ($type): stdClass {
262
                return (object) ['xref' => $xref, 'type' => $type];
263
            });
264
    }
265
}
266