1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Jkphl\Micrometa\Ports |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Micrometa\Ports; |
38
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Application\Service\ExtractorService; |
40
|
|
|
use Jkphl\Micrometa\Infrastructure\Factory\DocumentFactory; |
41
|
|
|
use Jkphl\Micrometa\Infrastructure\Factory\ItemFactory; |
42
|
|
|
use Jkphl\Micrometa\Infrastructure\Factory\ParserFactory; |
43
|
|
|
use Jkphl\Micrometa\Ports\Item\ItemObjectModel; |
44
|
|
|
use Jkphl\Micrometa\Ports\Item\ItemObjectModelInterface; |
45
|
|
|
use League\Uri\Schemes\Http; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Parser |
49
|
|
|
* |
50
|
|
|
* @package Jkphl\Micrometa |
51
|
|
|
* @subpackage Jkphl\Micrometa\Ports |
52
|
|
|
*/ |
53
|
|
|
class Parser |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* Micro information formats |
57
|
|
|
* |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
protected $formats; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Parser constructor |
64
|
|
|
* |
65
|
|
|
* @param int $formats Micro information formats to extract |
66
|
|
|
* @api |
67
|
|
|
*/ |
68
|
1 |
|
public function __construct($formats = null) |
69
|
|
|
{ |
70
|
1 |
|
$this->formats = $formats; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Extract micro information items out of a URI or piece of source |
75
|
|
|
* |
76
|
|
|
* @param string $uri URI |
77
|
|
|
* @param string $source Source code |
78
|
|
|
* @param int $formats Micro information formats to extract |
79
|
|
|
* @return ItemObjectModelInterface Item object model |
80
|
|
|
*/ |
81
|
1 |
|
public function __invoke($uri, $source = null, $formats = null) |
82
|
|
|
{ |
83
|
|
|
// If source code has been passed in |
84
|
1 |
|
$dom = (($source !== null) && strlen(trim($source))) ? |
85
|
1 |
|
DocumentFactory::createFromString($source) : DocumentFactory::createFromUri($uri); |
86
|
|
|
|
87
|
|
|
// Run through all format parsers |
88
|
1 |
|
$items = $rels = $alternates = []; |
|
|
|
|
89
|
1 |
|
$extractor = new ExtractorService(); |
90
|
1 |
|
foreach (ParserFactory::createParsersFromFormats( |
91
|
1 |
|
intval($formats ?: $this->formats), |
92
|
1 |
|
Http::createFromString($uri) |
93
|
|
|
) as $parser) { |
94
|
1 |
|
$results = $extractor->extract($dom, $parser); |
95
|
|
|
// print_r($results); |
96
|
1 |
|
$items += ItemFactory::createFromApplicationItems($results->getItems()); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return new ItemObjectModel($items); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.