Skip to content

Commit eb19c63

Browse files
committed
Fix a few phpstan issues
1 parent 488f601 commit eb19c63

17 files changed

+126
-85
lines changed

src/BibliographicRecord.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Scriptotek\Marc\Fields\Person;
1010
use Scriptotek\Marc\Fields\Publisher;
1111
use Scriptotek\Marc\Fields\Subject;
12-
use Scriptotek\Marc\Fields\SubjectFieldInterface;
12+
use Scriptotek\Marc\Fields\SubjectInterface;
1313
use Scriptotek\Marc\Fields\Title;
1414

1515
class BibliographicRecord extends Record
@@ -54,29 +54,29 @@ public function getIsbns(): array
5454
/**
5555
* Get the 245 field as a `Title` object. Returns null if no such field was found.
5656
*
57-
* @return Title
57+
* @return Title|null
5858
*/
59-
public function getTitle(): Title
59+
public function getTitle(): ?Title
6060
{
6161
return Title::get($this);
6262
}
6363

6464
/**
6565
* Get 250 as an `Edition` object. Returns null if no such field was found.
6666
*
67-
* @return Edition
67+
* @return Edition|null
6868
*/
69-
public function getEdition()
69+
public function getEdition(): ?Edition
7070
{
7171
return Edition::get($this);
7272
}
7373

7474
/**
7575
* Get 26[04]$b as a `Publisher` object. Returns null if no such field was found.
7676
*
77-
* @return Publisher
77+
* @return Publisher|null
7878
*/
79-
public function getPublisher()
79+
public function getPublisher(): ?Publisher
8080
{
8181
return Publisher::get($this);
8282
}
@@ -86,17 +86,17 @@ public function getPublisher()
8686
*
8787
* @return string
8888
*/
89-
public function getPubYear()
89+
public function getPubYear(): string
9090
{
9191
return substr($this->query('008')->text(), 7, 4);
9292
}
9393

9494
/**
9595
* Get TOC
9696
*
97-
* @return string
97+
* @return array|null
9898
*/
99-
public function getToc()
99+
public function getToc(): ?array
100100
{
101101
$field = $this->getField('505');
102102
if ($field) {
@@ -118,14 +118,15 @@ public function getToc()
118118
]);
119119
}
120120
}
121+
return null;
121122
}
122123

123124
/**
124125
* Get Summary
125126
*
126-
* @return array
127+
* @return array|null
127128
*/
128-
public function getSummary()
129+
public function getSummary(): array|null
129130
{
130131
$field = $this->getField('520');
131132
if ($field) {
@@ -134,21 +135,22 @@ public function getSummary()
134135
'c' => 'assigning_source',
135136
]);
136137
}
138+
return null;
137139
}
138140

139141
/**
140-
* Get an array of the 6XX fields as `SubjectFieldInterface` objects, optionally
142+
* Get an array of the 6XX fields as `SubjectInterface` objects, optionally
141143
* filtered by vocabulary and/or tag.
142144
*
143-
* @param string $vocabulary
144-
* @param string|string[] $tag
145-
* @return SubjectFieldInterface[]
145+
* @param string|null $vocabulary
146+
* @param string|string[]|null $tag
147+
* @return SubjectInterface[]
146148
*/
147-
public function getSubjects($vocabulary = null, $tag = null)
149+
public function getSubjects(string $vocabulary = null, array|string $tag = null): array
148150
{
149151
$tag = is_null($tag) ? [] : (is_array($tag) ? $tag : [$tag]);
150152

151-
return array_values(array_filter(Subject::get($this), function (SubjectFieldInterface $subject) use ($vocabulary, $tag) {
153+
return array_values(array_filter(Subject::get($this), function (SubjectInterface $subject) use ($vocabulary, $tag) {
152154
$a = is_null($vocabulary) || $vocabulary == $subject->getVocabulary();
153155
$b = empty($tag) || in_array($subject->getType(), $tag);
154156

@@ -160,10 +162,10 @@ public function getSubjects($vocabulary = null, $tag = null)
160162
* Get an array of the 080, 082, 083, 084 fields as `Classification` objects, optionally
161163
* filtered by scheme and/or tag.
162164
*
163-
* @param string $scheme
165+
* @param string|null $scheme
164166
* @return Classification[]
165167
*/
166-
public function getClassifications($scheme = null)
168+
public function getClassifications(string $scheme = null): array
167169
{
168170
return array_values(array_filter(Classification::get($this), function ($classifications) use ($scheme) {
169171
$a = is_null($scheme) || $scheme == $classifications->getScheme();
@@ -176,10 +178,10 @@ public function getClassifications($scheme = null)
176178
* Get an array of the 100 and 700 fields as `Person` objects, optionally
177179
* filtered by tag.
178180
*
179-
* @param string|string[] $tag
181+
* @param string|string[]|null $tag
180182
* @return Person[]
181183
*/
182-
public function getCreators($tag = null)
184+
public function getCreators(array|string $tag = null): array
183185
{
184186
$tag = is_null($tag) ? [] : (is_array($tag) ? $tag : [$tag]);
185187

@@ -191,9 +193,9 @@ public function getCreators($tag = null)
191193
/**
192194
* Get part of from 773.
193195
*
194-
* @return string
196+
* @return array|null
195197
*/
196-
public function getPartOf()
198+
public function getPartOf(): ?array
197199
{
198200
$field = $this->getField('773');
199201
if ($field) {
@@ -205,5 +207,6 @@ public function getPartOf()
205207
'v' => 'volume',
206208
]);
207209
}
210+
return null;
208211
}
209212
}

src/Fields/AuthorityFieldInterface.php renamed to src/Fields/AuthorityInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Scriptotek\Marc\Fields;
44

5-
interface AuthorityFieldInterface extends FieldInterface
5+
interface AuthorityInterface
66
{
77
/**
88
* The control number of the authority record.

src/Fields/Classification.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class Classification extends Subfield implements \JsonSerializable
88
{
99
use SerializableField;
1010

11-
const UDC = '080';
12-
const DEWEY = '082';
13-
const ADD_DEWEY = '082';
14-
const OTHER_SCHEME = '084';
11+
public const UDC = '080';
12+
public const DEWEY = '082';
13+
public const ADD_DEWEY = '082';
14+
public const OTHER_SCHEME = '084';
1515

1616
/**
1717
* @var array List of properties to be included when serializing the record using the `toArray()` method.

src/Fields/Corporation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Scriptotek\Marc\Record;
66

7-
class Corporation extends Field implements AuthorityFieldInterface
7+
class Corporation extends Field implements AuthorityInterface
88
{
99
/**
1010
* @var array List of properties to be included when serializing the record using the `toArray()` method.
@@ -13,8 +13,8 @@ class Corporation extends Field implements AuthorityFieldInterface
1313

1414
public static array $headingComponentCodes = ['a', 'b', 'c', 'd', 'n'];
1515

16-
const MAIN_ENTRY= '110';
17-
const ADDED_ENTRY = '710';
16+
public const MAIN_ENTRY= '110';
17+
public const ADDED_ENTRY = '710';
1818

1919
/**
2020
* @param Record $record

src/Fields/Field.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@
5252
* @method string getData()
5353
* @method bool setData(string $data)
5454
*/
55-
class Field implements JsonSerializable
55+
class Field implements JsonSerializable, FieldInterface
5656
{
57-
use SerializableField, MagicAccess;
57+
use SerializableField;
58+
use MagicAccess;
5859

5960
/**
6061
* @var array List of properties to be included when serializing the record
@@ -124,7 +125,7 @@ public function __call(string $name, array $args)
124125
*
125126
* @return string
126127
*/
127-
public function __toString()
128+
public function __toString(): string
128129
{
129130
return $this->field->__toString();
130131
}

src/Fields/FieldInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Scriptotek\Marc\Fields;
44

5+
use File_MARC_Field;
6+
57
interface FieldInterface
68
{
9+
public function __construct(File_MARC_Field $field);
10+
711
public function __toString(): string;
812

913
public function jsonSerialize();

src/Fields/Isbn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public function __toString(): string
1717
*/
1818
public static function get(Record $record): array
1919
{
20-
return parent::makeFieldObjects($record, '020');
20+
return static::makeFieldObjects($record, '020');
2121
}
2222
}

src/Fields/Person.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Scriptotek\Marc\Record;
66

7-
class Person extends Field implements FieldInterface, AuthorityFieldInterface
7+
class Person extends Field implements FieldInterface, AuthorityInterface
88
{
99
/**
1010
* @var array List of properties to be included when serializing the record using the `toArray()` method.
@@ -14,8 +14,8 @@ class Person extends Field implements FieldInterface, AuthorityFieldInterface
1414
public static string $formatWithDate = '{name} ({dates})';
1515
public static array $termComponentCodes = ['a', 'b', 'x', 'y', 'z'];
1616

17-
const MAIN_ENTRY= '100';
18-
const ADDED_ENTRY = '700';
17+
public const MAIN_ENTRY= '100';
18+
public const ADDED_ENTRY = '700';
1919

2020
/**
2121
* @param Record $record

src/Fields/SeeAlso.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Scriptotek\Marc\Fields;
4+
5+
use Scriptotek\Marc\Record;
6+
7+
class SeeAlso extends Field implements \JsonSerializable
8+
{
9+
/**
10+
* @param Record $record
11+
* @return array
12+
*/
13+
public static function get(Record $record): array
14+
{
15+
$seeAlsos = [];
16+
17+
$classMap = [
18+
'500' => Person::class,
19+
'510' => Corporation::class,
20+
// TODO: Add more classes
21+
'550' => Subject::class,
22+
];
23+
24+
foreach ($record->getFields('5..', true) as $field) {
25+
$tag = $field->getTag();
26+
if (isset($classMap[$tag])) {
27+
$seeAlsos[] = new $classMap[$tag]($field->getField());
28+
}
29+
}
30+
31+
return $seeAlsos;
32+
}
33+
}

src/Fields/Subfield.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function jsonSerialize(): string|array
3333
return (string) $this;
3434
}
3535

36-
public function __toString()
36+
public function __toString(): string
3737
{
3838
return $this->subfield->getData();
3939
}

0 commit comments

Comments
 (0)