1- <?php
1+ <?php declare (strict_types= 1 );
22
33/**
44 * RSS for PHP - small and easy-to-use library for consuming an RSS Feed
5- *
6- * @copyright Copyright (c) 2008 David Grudl
7- * @license New BSD License
85 */
96class Feed
107{
11- /** @var int */
12- public static $ cacheExpire = '1 day ' ;
13-
14- /** @var string */
15- public static $ cacheDir ;
16-
17- /** @var string */
18- public static $ userAgent = 'FeedFetcher-Google ' ;
19-
20- /** @var SimpleXMLElement */
21- protected $ xml ;
8+ public static string |int $ cacheExpire = '1 day ' ;
9+ public static ?string $ cacheDir = null ;
10+ public static string $ userAgent = 'FeedFetcher-Google ' ;
11+ protected SimpleXMLElement $ xml ;
2212
2313
2414 /**
@@ -56,7 +46,7 @@ public static function loadAtom(string $url, ?string $user = null, ?string $pass
5646 }
5747
5848
59- private static function fromRss (SimpleXMLElement $ xml )
49+ private static function fromRss (SimpleXMLElement $ xml ): static
6050 {
6151 if (!$ xml ->channel ) {
6252 throw new FeedException ('Invalid feed. ' );
@@ -76,16 +66,16 @@ private static function fromRss(SimpleXMLElement $xml)
7666 $ item ->timestamp = strtotime ($ item ->pubDate );
7767 }
7868 }
79- $ feed = new self ;
69+ $ feed = new static ;
8070 $ feed ->xml = $ xml ->channel ;
8171 return $ feed ;
8272 }
8373
8474
85- private static function fromAtom (SimpleXMLElement $ xml )
75+ private static function fromAtom (SimpleXMLElement $ xml ): static
8676 {
87- if (!in_array ('http://www.w3.org/2005/Atom ' , $ xml ->getDocNamespaces (), true )
88- && !in_array ('http://purl.org/atom/ns# ' , $ xml ->getDocNamespaces (), true )
77+ if (!in_array ('http://www.w3.org/2005/Atom ' , $ xml ->getDocNamespaces (), strict: true )
78+ && !in_array ('http://purl.org/atom/ns# ' , $ xml ->getDocNamespaces (), strict: true )
8979 ) {
9080 throw new FeedException ('Invalid feed. ' );
9181 }
@@ -95,41 +85,34 @@ private static function fromAtom(SimpleXMLElement $xml)
9585 $ entry ->url = (string ) $ entry ->link ['href ' ];
9686 $ entry ->timestamp = strtotime ($ entry ->updated );
9787 }
98- $ feed = new self ;
88+ $ feed = new static ;
9989 $ feed ->xml = $ xml ;
10090 return $ feed ;
10191 }
10292
10393
10494 /**
10595 * Returns property value. Do not call directly.
106- * @param string tag name
107- * @return SimpleXMLElement
10896 */
109- public function __get ($ name )
97+ public function __get (string $ name ): mixed
11098 {
11199 return $ this ->xml ->{$ name };
112100 }
113101
114102
115103 /**
116104 * Sets value of a property. Do not call directly.
117- * @param string property name
118- * @param mixed property value
119- * @return void
120105 */
121- public function __set ($ name , $ value )
106+ public function __set (string $ name , mixed $ value ): void
122107 {
123108 throw new Exception ("Cannot assign to a read-only property ' $ name'. " );
124109 }
125110
126111
127112 /**
128113 * Converts a SimpleXMLElement into an array.
129- * @param SimpleXMLElement
130- * @return array
131114 */
132- public function toArray (SimpleXMLElement $ xml = null )
115+ public function toArray (? SimpleXMLElement $ xml = null ): string | array
133116 {
134117 if ($ xml === null ) {
135118 $ xml = $ this ->xml ;
@@ -154,13 +137,9 @@ public function toArray(SimpleXMLElement $xml = null)
154137
155138 /**
156139 * Load XML from cache or HTTP.
157- * @param string
158- * @param string
159- * @param string
160- * @return SimpleXMLElement
161140 * @throws FeedException
162141 */
163- private static function loadXml ($ url , $ user , $ pass )
142+ private static function loadXml (string $ url , ? string $ user , ? string $ pass ): SimpleXMLElement
164143 {
165144 $ e = self ::$ cacheExpire ;
166145 $ cacheFile = self ::$ cacheDir . '/feed. ' . md5 (serialize (func_get_args ())) . '.xml ' ;
@@ -186,13 +165,8 @@ private static function loadXml($url, $user, $pass)
186165
187166 /**
188167 * Process HTTP request.
189- * @param string
190- * @param string
191- * @param string
192- * @return string|false
193- * @throws FeedException
194168 */
195- private static function httpRequest ($ url , $ user , $ pass )
169+ private static function httpRequest (string $ url , ? string $ user , ? string $ pass ): string | false
196170 {
197171 if (extension_loaded ('curl ' )) {
198172 $ curl = curl_init ();
@@ -201,12 +175,12 @@ private static function httpRequest($url, $user, $pass)
201175 curl_setopt ($ curl , CURLOPT_USERPWD , "$ user: $ pass " );
202176 }
203177 curl_setopt ($ curl , CURLOPT_USERAGENT , self ::$ userAgent ); // some feeds require a user agent
204- curl_setopt ($ curl , CURLOPT_HEADER , false );
178+ curl_setopt ($ curl , CURLOPT_HEADER , value: false );
205179 curl_setopt ($ curl , CURLOPT_TIMEOUT , 20 );
206180 curl_setopt ($ curl , CURLOPT_ENCODING , '' );
207- curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true ); // no echo, just return result
181+ curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , value: true ); // no echo, just return result
208182 if (!ini_get ('open_basedir ' )) {
209- curl_setopt ($ curl , CURLOPT_FOLLOWLOCATION , true ); // sometime is useful :)
183+ curl_setopt ($ curl , CURLOPT_FOLLOWLOCATION , value: true ); // sometime is useful :)
210184 }
211185 $ result = curl_exec ($ curl );
212186 return curl_errno ($ curl ) === 0 && curl_getinfo ($ curl , CURLINFO_HTTP_CODE ) === 200
@@ -232,10 +206,8 @@ private static function httpRequest($url, $user, $pass)
232206
233207 /**
234208 * Generates better accessible namespaced tags.
235- * @param SimpleXMLElement
236- * @return void
237209 */
238- private static function adjustNamespaces ($ el )
210+ private static function adjustNamespaces (SimpleXMLElement $ el ): void
239211 {
240212 foreach ($ el ->getNamespaces (true ) as $ prefix => $ ns ) {
241213 if ($ prefix === '' ) {
0 commit comments