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

FixMissingDeaths::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
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\Gedcom;
23
use Fisharebest\Webtrees\GedcomRecord;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Individual;
26
use Fisharebest\Webtrees\Services\DataFixService;
27
use Fisharebest\Webtrees\Tree;
28
use Illuminate\Database\Capsule\Manager as DB;
29
use Illuminate\Support\Collection;
30
31
/**
32
 * Class FixMissingDeaths
33
 */
34
class FixMissingDeaths extends AbstractModule implements ModuleDataFixInterface
35
{
36
    use ModuleDataFixTrait;
1 ignored issue
show
Bug introduced by
The trait Fisharebest\Webtrees\Module\ModuleDataFixTrait requires the property $xref which is not provided by Fisharebest\Webtrees\Module\FixMissingDeaths.
Loading history...
37
38
    /** @var DataFixService */
39
    private $data_fix_service;
40
41
    /**
42
     * FixMissingDeaths constructor.
43
     *
44
     * @param DataFixService $data_fix_service
45
     */
46
    public function __construct(DataFixService $data_fix_service)
47
    {
48
        $this->data_fix_service = $data_fix_service;
49
    }
50
51
    /**
52
     * How should this module be identified in the control panel, etc.?
53
     *
54
     * @return string
55
     */
56
    public function title(): string
57
    {
58
        /* I18N: Name of a module */
59
        return I18N::translate('Add missing death records');
60
    }
61
62
    /**
63
     * A sentence describing what this module does.
64
     *
65
     * @return string
66
     */
67
    public function description(): string
68
    {
69
        /* I18N: Description of a “Data fix” module */
70
        return I18N::translate('You can speed up the privacy calculations by adding a death record to individuals whose death can be inferred from other dates, but who do not have a record of death, burial, cremation, etc.');
71
    }
72
73
    /**
74
     * A list of all records that need examining.  This may include records
75
     * that do not need updating, if we can't detect this quickly using SQL.
76
     *
77
     * @param Tree                 $tree
78
     * @param array<string,string> $params
79
     *
80
     * @return Collection<string>|null
81
     */
82
    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

82
    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...
83
    {
84
        $query = DB::table('individuals')
85
            ->where('i_file', '=', $tree->id());
86
87
        foreach (Gedcom::DEATH_EVENTS as $event) {
88
            $query->where('i_gedcom', 'NOT LIKE', "%\n1 " . $event . '%');
89
        }
90
91
        return $query->pluck('i_id');
92
    }
93
94
    /**
95
     * Does a record need updating?
96
     *
97
     * @param GedcomRecord         $record
98
     * @param array<string,string> $params
99
     *
100
     * @return bool
101
     */
102
    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
103
    {
104
        return
105
            $record instanceof Individual &&
106
            $record->facts(Gedcom::DEATH_EVENTS, false, null, true)->isEmpty() &&
107
            $record->isDead();
108
    }
109
110
    /**
111
     * Show the changes we would make
112
     *
113
     * @param GedcomRecord         $record
114
     * @param array<string,string> $params
115
     *
116
     * @return string
117
     */
118
    public function previewUpdate(GedcomRecord $record, array $params): string
119
    {
120
        $old = $record->gedcom();
121
        $new = $this->updateGedcom($record);
122
123
        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
124
    }
125
126
    /**
127
     * Fix a record
128
     *
129
     * @param GedcomRecord         $record
130
     * @param array<string,string> $params
131
     *
132
     * @return void
133
     */
134
    public function updateRecord(GedcomRecord $record, array $params): void
135
    {
136
        $record->updateRecord($this->updateGedcom($record), false);
137
    }
138
139
    /**
140
     * @param GedcomRecord $record
141
     *
142
     * @return string
143
     */
144
    private function updateGedcom(GedcomRecord $record): string
145
    {
146
        return $record->gedcom() . "\n1 DEAT Y";
147
    }
148
}
149