railpage/railpagecore

View on GitHub
lib/News/Scraper.php

Summary

Maintainability
C
1 day
Test Coverage

Method fetch has 66 lines of code (exceeds 25 allowed). Consider refactoring.
Open

        public function fetch() {
            if (!is_string($this->feed)) {
                throw new Exception("Cannot fetch news articles from RSS feed because no RSS feed was provided");
            }
            
Severity: Major
Found in lib/News/Scraper.php - About 2 hrs to fix

    Function fetch has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

            public function fetch() {
                if (!is_string($this->feed)) {
                    throw new Exception("Cannot fetch news articles from RSS feed because no RSS feed was provided");
                }
                
    Severity: Minor
    Found in lib/News/Scraper.php - About 55 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function store has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
    Open

            public function store($logging = false) {
                
                /**
                 * Get Sphinx so we can lookup similar articles to prevent duplicates
                 */
    Severity: Minor
    Found in lib/News/Scraper.php - About 35 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    The method fetch() has 125 lines of code. Current threshold is set to 100. Avoid really long methods.
    Open

            public function fetch() {
                if (!is_string($this->feed)) {
                    throw new Exception("Cannot fetch news articles from RSS feed because no RSS feed was provided");
                }
                
    Severity: Minor
    Found in lib/News/Scraper.php by phpmd

    The class Scraper has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.
    Open

        class Scraper extends AppCore {
            
            /**
             * RSS feed URL
             * @since Version 3.9
    Severity: Minor
    Found in lib/News/Scraper.php by phpmd

    CouplingBetweenObjects

    Since: 1.1.0

    A class with too many dependencies has negative impacts on several quality aspects of a class. This includes quality criteria like stability, maintainability and understandability

    Example

    class Foo {
        /**
         * @var \foo\bar\X
         */
        private $x = null;
    
        /**
         * @var \foo\bar\Y
         */
        private $y = null;
    
        /**
         * @var \foo\bar\Z
         */
        private $z = null;
    
        public function setFoo(\Foo $foo) {}
        public function setBar(\Bar $bar) {}
        public function setBaz(\Baz $baz) {}
    
        /**
         * @return \SplObjectStorage
         * @throws \OutOfRangeException
         * @throws \InvalidArgumentException
         * @throws \ErrorException
         */
        public function process(\Iterator $it) {}
    
        // ...
    }

    Source https://phpmd.org/rules/design.html#couplingbetweenobjects

    The method store has a boolean flag argument $logging, which is a certain sign of a Single Responsibility Principle violation.
    Open

            public function store($logging = false) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpmd

    BooleanArgumentFlag

    Since: 1.4.0

    A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

    Example

    class Foo {
        public function bar($flag = true) {
        }
    }

    Source https://phpmd.org/rules/cleancode.html#booleanargumentflag

    The method __construct has a boolean flag argument $url, which is a certain sign of a Single Responsibility Principle violation.
    Open

            public function __construct($url = false, $provider = "Railway Gazette") {
    Severity: Minor
    Found in lib/News/Scraper.php by phpmd

    BooleanArgumentFlag

    Since: 1.4.0

    A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

    Example

    class Foo {
        public function bar($flag = true) {
        }
    }

    Source https://phpmd.org/rules/cleancode.html#booleanargumentflag

    The method fetch uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                    } else {
                        $content = $item->description->__toString();
                        $content = strip_tags($content, "img,a");
                    }
    Severity: Minor
    Found in lib/News/Scraper.php by phpmd

    ElseExpression

    Since: 1.4.0

    An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($flag) {
                // one branch
            } else {
                // another branch
            }
        }
    }

    Source https://phpmd.org/rules/cleancode.html#elseexpression

    The method fetch() contains an exit expression.
    Open

                    printArray($row);die;
    Severity: Minor
    Found in lib/News/Scraper.php by phpmd

    ExitExpression

    Since: 0.2

    An exit-expression within regular code is untestable and therefore it should be avoided. Consider to move the exit-expression into some kind of startup script where an error/exception code is returned to the calling environment.

    Example

    class Foo {
        public function bar($param)  {
            if ($param === 42) {
                exit(23);
            }
        }
    }

    Source https://phpmd.org/rules/design.html#exitexpression

    The method fetch() contains an exit expression.
    Open

                printArray($items);die;
    Severity: Minor
    Found in lib/News/Scraper.php by phpmd

    ExitExpression

    Since: 0.2

    An exit-expression within regular code is untestable and therefore it should be avoided. Consider to move the exit-expression into some kind of startup script where an error/exception code is returned to the calling environment.

    Example

    class Foo {
        public function bar($param)  {
            if ($param === 42) {
                exit(23);
            }
        }
    }

    Source https://phpmd.org/rules/design.html#exitexpression

    Identical blocks of code found in 5 locations. Consider refactoring.
    Open

                $config = array(
                    'adapter' => 'Zend\Http\Client\Adapter\Curl',
                    'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
                );
    Severity: Major
    Found in lib/News/Scraper.php and 4 other locations - About 1 hr to fix
    lib/Bugs/Bugs.php on lines 51..54
    lib/GTFS/AU/VIC/PTV/PTV.php on lines 278..281
    lib/Graphite/Graph.php on lines 87..90
    lib/Jobs/Scraper.php on lines 202..205

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 30.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 8 locations. Consider refactoring.
    Open

                if (!is_string($this->feed)) {
                    throw new Exception("Cannot fetch news articles from RSS feed because no RSS feed was provided");
                }
    Severity: Major
    Found in lib/News/Scraper.php and 7 other locations - About 45 mins to fix
    lib/Forums/Thread.php on lines 445..447
    lib/Images/Image.php on lines 446..448
    lib/Jobs/Scraper.php on lines 96..98
    lib/Jobs/Scraper.php on lines 192..194
    lib/Locos/Date.php on lines 258..260
    lib/Newsletters/Newsletter.php on lines 147..149
    lib/Notifications/Transport/Email.php on lines 167..169

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 26.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Identical blocks of code found in 4 locations. Consider refactoring.
    Open

                    $query = $Sphinx->select("*")
                            ->from("idx_news_article")
                            ->orderBy("story_time_unix", "DESC")
                            ->where("story_time_unix", ">=", $article['date']->sub(new DateInterval("P7D"))->getTimestamp())
    Severity: Major
    Found in lib/News/Scraper.php and 3 other locations - About 40 mins to fix
    lib/News/Article.php on lines 1008..1011
    lib/News/Base.php on lines 67..70
    lib/News/Topic.php on lines 266..269

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 23.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 3 locations. Consider refactoring.
    Open

                    $topic = json_decode(json_encode($item->category), true);
    Severity: Minor
    Found in lib/News/Scraper.php and 2 other locations - About 40 mins to fix
    lib/Notifications/Notification.php on lines 368..368
    lib/Users/User.php on lines 2769..2769

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 23.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 6 locations. Consider refactoring.
    Open

                if (is_string($url)) {
                    $this->feed = $url;
                }
    Severity: Major
    Found in lib/News/Scraper.php and 5 other locations - About 30 mins to fix
    lib/GTFS/StandardRoute.php on lines 60..62
    lib/Jobs/Scraper.php on lines 73..75
    lib/Jobs/Scraper.php on lines 77..79
    lib/News/Scraper.php on lines 74..76
    lib/Users/User.php on lines 1448..1450

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 21.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 6 locations. Consider refactoring.
    Open

                if (is_string($provider)) {
                    $this->provider = $provider;
                }
    Severity: Major
    Found in lib/News/Scraper.php and 5 other locations - About 30 mins to fix
    lib/GTFS/StandardRoute.php on lines 60..62
    lib/Jobs/Scraper.php on lines 73..75
    lib/Jobs/Scraper.php on lines 77..79
    lib/News/Scraper.php on lines 70..72
    lib/Users/User.php on lines 1448..1450

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 21.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 3 locations. Consider refactoring.
    Open

                    if (isset($ns['content']) && !empty($ns['content'])) {
                        $content = $item->children($ns['content']);
                        $content = strval($content->encoded);
                    } else {
                        $content = $item->description->__toString();
    Severity: Minor
    Found in lib/News/Scraper.php and 2 other locations - About 30 mins to fix
    lib/Locos/Gauge.php on lines 116..116
    lib/Newsletters/Weekly.php on lines 436..436

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 20.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Each PHP statement must be on a line by itself
    Open

                printArray($items);die;
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Each PHP statement must be on a line by itself
    Open

                    printArray($row);die;
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Blank line found at start of control structure
    Open

                foreach ($items as $Item) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Blank line found at start of control structure
    Open

                foreach ($xml->channel->item as $item) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    if (!count($matches) && !count($rejected)) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        class Scraper extends AppCore {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                if (!is_string($this->feed)) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                if (is_string($url)) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                if (is_string($provider)) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                foreach ($xml->channel->item as $item) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            private $provider;
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            public function fetch() {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            public function store($logging = false) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            private $articles;
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                foreach ($items as $Item) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    } else {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            private $feed;
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            public function __construct($url = false, $provider = "Railway Gazette") {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                foreach ($this->articles as $article) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    if (empty($topic)) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 16 spaces, found 20
    Open

                        if ($logging) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 16 spaces, found 20
    Open

                        }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    if (isset($ns['content']) && !empty($ns['content'])) {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            public function getArticles() {
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    }
    Severity: Minor
    Found in lib/News/Scraper.php by phpcodesniffer

    There are no issues that match your filters.

    Category
    Status