Completed
Push — develop ( 2ca406...b33d79 )
by Greg
27:09 queued 11:25
created

Functions::cousinName2()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 67
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 41
nop 3
dl 0
loc 67
rs 4.8666
c 0
b 0
f 0
nc 18

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Functions;
21
22
use Exception;
23
use Fisharebest\Webtrees\Auth;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Individual;
26
27
/**
28
 * Class Functions - common functions
29
 */
30
class Functions
31
{
32
    /**
33
     * Convert a file upload PHP error code into user-friendly text.
34
     *
35
     * @param int $error_code
36
     *
37
     * @return string
38
     */
39
    public static function fileUploadErrorText(int $error_code): string
40
    {
41
        switch ($error_code) {
42
            case UPLOAD_ERR_OK:
43
                return I18N::translate('File successfully uploaded');
44
            case UPLOAD_ERR_INI_SIZE:
45
            case UPLOAD_ERR_FORM_SIZE:
46
                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
47
                return I18N::translate('The uploaded file exceeds the allowed size.');
48
            case UPLOAD_ERR_PARTIAL:
49
                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
50
                return I18N::translate('The file was only partially uploaded. Please try again.');
51
            case UPLOAD_ERR_NO_FILE:
52
                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
53
                return I18N::translate('No file was received. Please try again.');
54
            case UPLOAD_ERR_NO_TMP_DIR:
55
                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
56
                return I18N::translate('The PHP temporary folder is missing.');
57
            case UPLOAD_ERR_CANT_WRITE:
58
                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
59
                return I18N::translate('PHP failed to write to disk.');
60
            case UPLOAD_ERR_EXTENSION:
61
                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
62
                return I18N::translate('PHP blocked the file because of its extension.');
63
            default:
64
                return 'Error: ' . $error_code;
65
        }
66
    }
67
68
    /**
69
     * get a gedcom subrecord
70
     *
71
     * searches a gedcom record and returns a subrecord of it. A subrecord is defined starting at a
72
     * line with level N and all subsequent lines greater than N until the next N level is reached.
73
     * For example, the following is a BIRT subrecord:
74
     * <code>1 BIRT
75
     * 2 DATE 1 JAN 1900
76
     * 2 PLAC Phoenix, Maricopa, Arizona</code>
77
     * The following example is the DATE subrecord of the above BIRT subrecord:
78
     * <code>2 DATE 1 JAN 1900</code>
79
     *
80
     * @param int    $level   the N level of the subrecord to get
81
     * @param string $tag     a gedcom tag or string to search for in the record (ie 1 BIRT or 2 DATE)
82
     * @param string $gedrec  the parent gedcom record to search in
83
     * @param int    $num     this allows you to specify which matching <var>$tag</var> to get. Oftentimes a
84
     *                        gedcom record will have more that 1 of the same type of subrecord. An individual may have
85
     *                        multiple events for example. Passing $num=1 would get the first 1. Passing $num=2 would get the
86
     *                        second one, etc.
87
     *
88
     * @return string the subrecord that was found or an empty string "" if not found.
89
     */
90
    public static function getSubRecord(int $level, string $tag, string $gedrec, int $num = 1): string
91
    {
92
        if ($gedrec === '') {
93
            return '';
94
        }
95
        // -- adding \n before and after gedrec
96
        $gedrec       = "\n" . $gedrec . "\n";
97
        $tag          = trim($tag);
98
        $searchTarget = "~[\n]" . $tag . "[\s]~";
99
        $ct           = preg_match_all($searchTarget, $gedrec, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
100
        if ($ct === 0) {
101
            return '';
102
        }
103
        if ($ct < $num) {
104
            return '';
105
        }
106
        $pos1 = $match[$num - 1][0][1];
107
        $pos2 = strpos($gedrec, "\n$level", $pos1 + 1);
108
        if (!$pos2) {
109
            $pos2 = strpos($gedrec, "\n1", $pos1 + 1);
110
        }
111
        if (!$pos2) {
112
            $pos2 = strpos($gedrec, "\nWT_", $pos1 + 1); // WT_SPOUSE, WT_FAMILY_ID ...
113
        }
114
        if (!$pos2) {
115
            return ltrim(substr($gedrec, $pos1));
116
        }
117
        $subrec = substr($gedrec, $pos1, $pos2 - $pos1);
118
119
        return ltrim($subrec);
120
    }
121
122
    /**
123
     * get CONT lines
124
     *
125
     * get the N+1 CONT or CONC lines of a gedcom subrecord
126
     *
127
     * @param int    $nlevel the level of the CONT lines to get
128
     * @param string $nrec   the gedcom subrecord to search in
129
     *
130
     * @return string a string with all CONT lines merged
131
     */
132
    public static function getCont(int $nlevel, string $nrec): string
133
    {
134
        $text = '';
135
136
        $subrecords = explode("\n", $nrec);
137
        foreach ($subrecords as $thisSubrecord) {
138
            if (substr($thisSubrecord, 0, 2) !== $nlevel . ' ') {
139
                continue;
140
            }
141
            $subrecordType = substr($thisSubrecord, 2, 4);
142
            if ($subrecordType === 'CONT') {
143
                $text .= "\n" . substr($thisSubrecord, 7);
144
            }
145
        }
146
147
        return $text;
148
    }
149
}
150