Completed
Pull Request — develop_3.0 (#438)
by Adrien
02:55
created

Sheet   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 78
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getIndex() 0 4 1
A getAssociatedWorkbookId() 0 4 1
A getName() 0 4 1
A setName() 0 10 1
1
<?php
2
3
namespace Box\Spout\Writer\Common\Entity;
4
5
use Box\Spout\Writer\Common\Manager\SheetManager;
6
7
/**
8
 * Class Sheet
9
 * External representation of a worksheet
10
 *
11
 * @package Box\Spout\Writer\Common\Entity
12
 */
13
class Sheet
14
{
15
    const DEFAULT_SHEET_NAME_PREFIX = 'Sheet';
16
17
    /** @var int Index of the sheet, based on order in the workbook (zero-based) */
18
    private $index;
19
20
    /** @var string ID of the sheet's associated workbook. Used to restrict sheet name uniqueness enforcement to a single workbook */
21
    private $associatedWorkbookId;
22
23
    /** @var string Name of the sheet */
24
    private $name;
25
26
    /**
27
     * @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
28
     * @param string $associatedWorkbookId ID of the sheet's associated workbook
29
     * @param SheetManager $sheetManager
30
     */
31 107
    public function __construct($sheetIndex, $associatedWorkbookId, SheetManager $sheetManager)
32
    {
33 107
        $this->index = $sheetIndex;
34 107
        $this->associatedWorkbookId = $associatedWorkbookId;
35
36 107
        $this->sheetManager = $sheetManager;
0 ignored issues
show
Bug introduced by
The property sheetManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37 107
        $this->sheetManager->markWorkbookIdAsUsed($associatedWorkbookId);
38
39 107
        $this->setName(self::DEFAULT_SHEET_NAME_PREFIX . ($sheetIndex + 1));
40 107
    }
41
42
    /**
43
     * @api
44
     * @return int Index of the sheet, based on order in the workbook (zero-based)
45
     */
46 107
    public function getIndex()
47
    {
48 107
        return $this->index;
49
    }
50
51
    /**
52
     * @return string
53
     */
54 107
    public function getAssociatedWorkbookId()
55
    {
56 107
        return $this->associatedWorkbookId;
57
    }
58
59
    /**
60
     * @api
61
     * @return string Name of the sheet
62
     */
63 107
    public function getName()
64
    {
65 107
        return $this->name;
66
    }
67
68
    /**
69
     * Sets the name of the sheet. Note that Excel has some restrictions on the name:
70
     *  - it should not be blank
71
     *  - it should not exceed 31 characters
72
     *  - it should not contain these characters: \ / ? * : [ or ]
73
     *  - it should be unique
74
     *
75
     * @api
76
     * @param string $name Name of the sheet
77
     * @return Sheet
78
     * @throws \Box\Spout\Writer\Exception\InvalidSheetNameException If the sheet's name is invalid.
79
     */
80 107
    public function setName($name)
81
    {
82 107
        $this->sheetManager->throwIfNameIsInvalid($name, $this);
83
84 107
        $this->name = $name;
85
86 107
        $this->sheetManager->markSheetNameAsUsed($this);
87
88 107
        return $this;
89
    }
90
}
91