1010use ArrayAccess \DnsRecord \Interfaces \ResourceRecord \ResourceRecordMetaTypeInterface ;
1111use ArrayAccess \DnsRecord \Interfaces \ResourceRecord \ResourceRecordQTypeDefinitionInterface ;
1212use ArrayAccess \DnsRecord \Interfaces \ResourceRecord \ResourceRecordTypeInterface ;
13- use ArrayAccess \DnsRecord \ResourceRecord \Definitions \QType ;
1413use ArrayAccess \DnsRecord \Utils \Buffer ;
1514use ArrayAccess \DnsRecord \Utils \Lookup ;
16- use function is_string ;
15+ use function is_array ;
1716use function ord ;
1817use function serialize ;
1918use function sprintf ;
@@ -47,11 +46,11 @@ abstract class AbstractResourceRecordType implements ResourceRecordTypeInterface
4746 /**
4847 * Response type
4948 *
50- * @var string| ResourceRecordQTypeDefinitionInterface
49+ * @var ResourceRecordQTypeDefinitionInterface
5150 * @see ResourceRecordTypeInterface::getType()
5251 * @see Lookup::RR_TYPES
5352 */
54- protected ResourceRecordQTypeDefinitionInterface | string $ type ;
53+ protected ResourceRecordQTypeDefinitionInterface $ type ;
5554
5655 /**
5756 * @var ResourceRecordClassInterface
@@ -112,10 +111,6 @@ public function getOffsetPosition(): int
112111 protected function parseMessage (): void
113112 {
114113 $ type = static ::TYPE ;
115- if (!isset ($ this ->type ) && is_string ($ type )) {
116- $ this ->type = $ type ;
117- }
118-
119114 $ message = $ this ->message ->getMessage ();
120115 $ offsetPosition = $ this ->offsetPosition ;
121116 $ this ->name = Buffer::readLabel ($ message , $ offsetPosition );
@@ -125,28 +120,32 @@ protected function parseMessage(): void
125120 'Response header length is invalid '
126121 );
127122 }
128- [
129- 'type ' => $ type ,
130- 'class ' => $ class ,
131- 'ttl ' => $ this ->ttl ,
132- 'length ' => $ this ->rdLength ,
133- ] = unpack ("ntype/nclass/Nttl/nlength " , $ this ->header );
123+ $ headerArray = unpack ("ntype/nclass/Nttl/nlength " , $ this ->header );
124+ $ this ->rdLength = 0 ;
125+ if (is_array ($ headerArray )) {
126+ [
127+ 'type ' => $ type ,
128+ 'class ' => $ class ,
129+ 'ttl ' => $ this ->ttl ,
130+ 'length ' => $ this ->rdLength ,
131+ ] = $ headerArray ;
132+ }
133+
134134 $ this ->rData = substr ($ message , $ offsetPosition , $ this ->rdLength );
135135 if (strlen ($ this ->rData ) !== $ this ->rdLength ) {
136136 throw new LengthException (
137137 'Rdata & length from response header is mismatch '
138138 );
139139 }
140140
141- $ type = Lookup::resourceType ($ type );
142- $ class = Lookup::resourceClass ($ class );
143- if (isset ($ this ->type )) {
144- $ originType = $ this ->getType ();
145- if ($ originType ->getName () !== $ type ->getName ()) {
141+ $ type = $ type ? Lookup::resourceType ($ type ) : null ;
142+ $ class = Lookup::resourceClass ($ class ??'' );
143+ if (isset ($ this ->type ) || !$ type ) {
144+ if ($ this ->type ->getName () !== $ type ?->getName()) {
146145 throw new MalformedDataException (
147146 sprintf (
148147 'Response type does not match with current object type. object type: [%s] response type: [%s] ' ,
149- $ originType ,
148+ $ this -> type -> getName () ,
150149 $ type
151150 )
152151 );
@@ -165,6 +164,7 @@ protected function parseMessage(): void
165164 * @param string $message
166165 * @param int $rdataOffset
167166 * @noinspection PhpMissingReturnTypeInspection
167+ * @phpstan-ignore-next-line
168168 */
169169 protected function parseRData (string $ message , int $ rdataOffset )
170170 {
@@ -212,11 +212,6 @@ public function getMessage(): PacketMessageInterface
212212 */
213213 public function getType (): ResourceRecordQTypeDefinitionInterface
214214 {
215- if (isset ($ this ->type )) {
216- is_string ($ this ->type ) && $ this ->type = QType::create ($ this ->type );
217- return $ this ->type ;
218- }
219-
220215 return $ this ->type ;
221216 }
222217
@@ -294,7 +289,7 @@ public function __serialize(): array
294289 /**
295290 * Magic method for unserialize
296291 *
297- * @param array $data
292+ * @param array{message: PacketMessageInterface, offsetPosition: int} $data
298293 * @return void
299294 * @throws MalformedDataException
300295 */
0 commit comments