Completed
Push — master ( 2b5604...0a5d2b )
by Burhan
17s queued 11s
created

FieldSorter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 34
ccs 4
cts 5
cp 0.8
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A sort() 0 15 2
1
<?php
2
3
namespace Graze\CiffRenderer\Parser;
4
5
use SimpleXMLElement;
6
7
class FieldSorter
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $fieldTypeToOrder = [
13
        FieldType::FIELD_STATIC_GRAPHIC    => 1,
14
        FieldType::FIELD_FIXED_TEXT        => 2,
15
        FieldType::FIELD_COMPLEX_TEXT      => 3,
16
        FieldType::FIELD_BARCODE           => 4,
17
        FieldType::FIELD_DATE              => 5,
18
        FieldType::FIELD_OFFSET_DATE       => 6,
19
        FieldType::FIELD_MERGE_FIELD       => 7,
20
        FieldType::FIELD_GRAPHIC_PRIMITIVE => 8,
21
    ];
22
23
    /**
24
     * @param SimpleXMLElement[] $fields
25
     */
26
    public function sort(array &$fields)
27
    {
28 1
        usort($fields, function ($field1, $field2) {
29
            // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
30 1
            $typeComparison = $this->fieldTypeToOrder[(string) $field1->FldType] - $this->fieldTypeToOrder[(string) $field2->FldType];
31
32 1
            if ($typeComparison === 0) {
33
                // Same field type, so order by field name
34
                /**
35
                 * @var SimpleXMLElement $field1
36
                 * @var SimpleXMLElement $field2
37
                 */
38
                return strcmp((string)$field1->attributes()['Name'], (string)$field2->attributes()['Name']);
39
            } else {
40 1
                return $typeComparison;
41
            }
42 1
        });
43 1
    }
44
}
45